Skip to content

Commit 20d5f1e

Browse files
fix: wrong project folder detection (#57)
1 parent 25124b8 commit 20d5f1e

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/utils.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ export async function findProjectDir(
8282
pkgJsonPath: null,
8383
},
8484
): Promise<ProjectInfo> {
85+
// Ensure we check for `package.json` first as this defines
86+
// the root project location.
87+
if (result.pkgJsonPath === null) {
88+
const pkgJsonPath = path.join(dir, "package.json");
89+
if (await fileExists(pkgJsonPath)) {
90+
logDebug(`Found package.json at ${pkgJsonPath}`);
91+
logDebug(`Setting project directory to ${dir}`);
92+
result.projectDir = dir;
93+
result.pkgJsonPath = pkgJsonPath;
94+
}
95+
}
96+
8597
const npmLockfile = path.join(dir, "package-lock.json");
8698
if (await fileExists(npmLockfile)) {
8799
logDebug(`Detected npm from lockfile ${npmLockfile}`);
@@ -113,15 +125,6 @@ export async function findProjectDir(
113125
return result;
114126
}
115127

116-
if (result.pkgJsonPath === null) {
117-
const pkgJsonPath = path.join(dir, "package.json");
118-
if (await fileExists(pkgJsonPath)) {
119-
logDebug(`Found package.json at ${pkgJsonPath}`);
120-
result.projectDir = dir;
121-
result.pkgJsonPath = pkgJsonPath;
122-
}
123-
}
124-
125128
const prev = dir;
126129
dir = path.dirname(dir);
127130
if (dir === prev) {

test/commands.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from "./test_utils";
1313
import * as assert from "node:assert/strict";
1414
import {
15+
exec,
1516
PkgJson,
1617
readJson,
1718
readTextFile,
@@ -289,6 +290,23 @@ describe("install", () => {
289290
);
290291
});
291292

293+
it("pnpm install into existing project", async () => {
294+
await runInTempDir(async (dir) => {
295+
const sub = path.join(dir, "sub", "sub1");
296+
await fs.promises.mkdir(sub, {
297+
recursive: true,
298+
});
299+
300+
await exec("pnpm", ["i", "preact"], dir);
301+
302+
await runJsr(["i", "--pnpm", "@std/encoding@0.216.0"], sub);
303+
assert.ok(
304+
await isFile(path.join(dir, "pnpm-lock.yaml")),
305+
"pnpm lockfile not created",
306+
);
307+
});
308+
});
309+
292310
if (process.platform !== "win32") {
293311
it("jsr add --bun @std/encoding@0.216.0 - forces bun", async () => {
294312
await withTempEnv(

0 commit comments

Comments
 (0)