diff --git a/src/utils.ts b/src/utils.ts index b33478c..3e9f4f0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -82,6 +82,18 @@ export async function findProjectDir( pkgJsonPath: null, }, ): Promise { + // Ensure we check for `package.json` first as this defines + // the root project location. + if (result.pkgJsonPath === null) { + const pkgJsonPath = path.join(dir, "package.json"); + if (await fileExists(pkgJsonPath)) { + logDebug(`Found package.json at ${pkgJsonPath}`); + logDebug(`Setting project directory to ${dir}`); + result.projectDir = dir; + result.pkgJsonPath = pkgJsonPath; + } + } + const npmLockfile = path.join(dir, "package-lock.json"); if (await fileExists(npmLockfile)) { logDebug(`Detected npm from lockfile ${npmLockfile}`); @@ -113,15 +125,6 @@ export async function findProjectDir( return result; } - if (result.pkgJsonPath === null) { - const pkgJsonPath = path.join(dir, "package.json"); - if (await fileExists(pkgJsonPath)) { - logDebug(`Found package.json at ${pkgJsonPath}`); - result.projectDir = dir; - result.pkgJsonPath = pkgJsonPath; - } - } - const prev = dir; dir = path.dirname(dir); if (dir === prev) { diff --git a/test/commands.test.ts b/test/commands.test.ts index 91c6fbe..eba49dc 100644 --- a/test/commands.test.ts +++ b/test/commands.test.ts @@ -12,6 +12,7 @@ import { } from "./test_utils"; import * as assert from "node:assert/strict"; import { + exec, PkgJson, readJson, readTextFile, @@ -289,6 +290,23 @@ describe("install", () => { ); }); + it("pnpm install into existing project", async () => { + await runInTempDir(async (dir) => { + const sub = path.join(dir, "sub", "sub1"); + await fs.promises.mkdir(sub, { + recursive: true, + }); + + await exec("pnpm", ["i", "preact"], dir); + + await runJsr(["i", "--pnpm", "@std/encoding@0.216.0"], sub); + assert.ok( + await isFile(path.join(dir, "pnpm-lock.yaml")), + "pnpm lockfile not created", + ); + }); + }); + if (process.platform !== "win32") { it("jsr add --bun @std/encoding@0.216.0 - forces bun", async () => { await withTempEnv(