Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge 0.61.2 hotfix back into main #4703

Merged
merged 7 commits into from
Oct 11, 2024
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ developer/

*.vsix
*.zip
vite.config.ts.timestamp-*


# Astro
.astro/
Expand Down
14 changes: 14 additions & 0 deletions packages/compiler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Change Log - @typespec/compiler

## 0.61.2

### Bug Fixes

- [#4704](https://github.com/microsoft/typespec/pull/4704) Fix order of resolution from node_modules and parent package


## 0.61.1

### Bug Fixes

- [#4697](https://github.com/microsoft/typespec/pull/4697) Fix module resolution when resolving self from within another package


## 0.61.0

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion packages/compiler/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@typespec/compiler",
"version": "0.61.0",
"version": "0.61.2",
"description": "TypeSpec Compiler Preview",
"author": "Microsoft Corporation",
"license": "MIT",
Expand Down
33 changes: 14 additions & 19 deletions packages/compiler/src/module-resolver/module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
InvalidPackageTargetError,
NoMatchingConditionsError,
} from "./esm/utils.js";
import { parseNodeModuleSpecifier } from "./utils.js";
import { NodeModuleSpecifier, parseNodeModuleSpecifier } from "./utils.js";

// Resolve algorithm of node https://nodejs.org/api/modules.html#modules_all_together

Expand Down Expand Up @@ -126,10 +126,6 @@ export async function resolveModule(
}
}

// Try to resolve package itself.
const self = await resolveSelf(specifier, absoluteStart);
if (self) return self;

// Try to resolve as a node_module package.
const module = await resolveAsNodeModule(specifier, absoluteStart);
if (module) return module;
Expand All @@ -154,21 +150,18 @@ export async function resolveModule(
}

/**
* Equivalent implementation to node LOAD_PACKAGE_SELF
* Resolve if the import is importing the current package.
* Try to import a package with that name in the current directory.
*/
async function resolveSelf(name: string, baseDir: string): Promise<ResolvedModule | undefined> {
for (const dir of listDirHierarchy(baseDir)) {
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;
}
}
return undefined;
async function resolveSelf(
{ packageName, subPath }: NodeModuleSpecifier,
dir: string,
): Promise<ResolvedModule | undefined> {
const pkgFile = resolvePath(dir, "package.json");
if (!(await isFile(host, pkgFile))) return undefined;
const pkg = await readPackage(host, pkgFile);
// 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 !== packageName) return undefined;
return loadPackage(dir, pkg, subPath);
}

/**
Expand All @@ -184,6 +177,8 @@ export async function resolveModule(
const dirs = listDirHierarchy(baseDir);

for (const dir of dirs) {
const self = await resolveSelf(module, dir);
if (self) return self;
const n = await loadPackageAtPath(
joinPaths(dir, "node_modules", module.packageName),
module.subPath,
Expand Down
8 changes: 4 additions & 4 deletions packages/compiler/templates/scaffolding.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"title": "Empty project",
"description": "Create an empty project.",
"libraries": [],
"compilerVersion": "0.61.0"
"compilerVersion": "0.61.2"
},
"rest": {
"title": "Generic REST API",
"description": "Create a project representing a generic REST API",
"compilerVersion": "0.61.0",
"compilerVersion": "0.61.2",
"libraries": [
"@typespec/http",
"@typespec/rest",
Expand All @@ -23,7 +23,7 @@
"library-ts": {
"title": "TypeSpec Library (With TypeScript)",
"description": "Create a new package to add decorators or linters to typespec.",
"compilerVersion": "0.61.0",
"compilerVersion": "0.61.2",
"libraries": [],
"files": [
{
Expand Down Expand Up @@ -99,7 +99,7 @@
"emitter-ts": {
"title": "TypeSpec Emitter (With TypeScript)",
"description": "Create a new package that will be emitting typespec",
"compilerVersion": "0.61.0",
"compilerVersion": "0.61.2",
"libraries": [],
"files": [
{
Expand Down
53 changes: 53 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,56 @@ 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",
});
});

it("prioritize local node_modules over self from multiple parent up", async () => {
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": "",
// @scope/proj installed locally
"/ws/proj/node_modules/test-lib/node_modules/@scope/proj/package.json": JSON.stringify({
name: "@scope/proj",
main: "entry.js",
}),
"/ws/proj/node_modules/test-lib/node_modules/@scope/proj/entry.js": "",
});

const resolved = await resolveModule(host, "@scope/proj", {
baseDir: "/ws/proj/node_modules/test-lib/nested",
});
const path = "/ws/proj/node_modules/test-lib/node_modules/@scope/proj";
expect(resolved).toMatchObject({
type: "module",
path,
mainFile: `${path}/entry.js`,
});
});
});
Loading