Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/compiler"
---

Fix module resolution when resolving self from within another package
8 changes: 3 additions & 5 deletions packages/compiler/src/module-resolver/module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,9 @@ export async function resolveModule(
const pkgFile = resolvePath(dir, "package.json");
if (!(await isFile(host, pkgFile))) continue;
const pkg = await readPackage(host, pkgFile);
if (pkg.name === name) {
return loadPackage(dir, pkg);
} else {
return undefined;
}
// Node Spec says that you shouldn't lookup past the first package.json. However we used to support that so keeping this.
if (pkg.name !== name) continue;
return loadPackage(dir, pkg);
}
return undefined;
}
Expand Down
26 changes: 26 additions & 0 deletions packages/compiler/test/module-resolver/module-resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,3 +374,29 @@ describe("packages", () => {
});
});
});

describe("resolve self", () => {
const { host } = mkFs({
"/ws/proj/package.json": JSON.stringify({ name: "@scope/proj", main: "entry.js" }),
"/ws/proj/entry.js": "",
"/ws/proj/nested/index.js": "",
"/ws/proj/node_modules/test-lib/package.json": JSON.stringify({ main: "entry.js" }),
"/ws/proj/node_modules/test-lib/entry.js": "",
"/ws/proj/node_modules/test-lib/nested/index.js": "",
});

it.each([
["at the same level", "/ws/proj"],
["nested", "/ws/proj/nested"],
["lookup parent package.json", "/ws/proj/node_modules/test-lib/nested"],
])("%s", async (_, baseDir) => {
const resolved = await resolveModule(host, "@scope/proj", {
baseDir,
});
expect(resolved).toMatchObject({
type: "module",
path: "/ws/proj",
mainFile: "/ws/proj/entry.js",
});
});
});