Skip to content

Commit 54b4e80

Browse files
authored
Don't include the version in module paths when serializing functions when using pnpm (#23767)
pnpm installs packages into a store keyed by version numbers `node_modules/.pnpm/<pkg>@<version>/node_modules/<pkg>/...`, and adds a symlink to this in `node_modules/<pkg>` so Node.js can resolve the package. The closure serializer sees the real path, not the symlink, and so would write out code like ```javascript cosnt ms = require(".pnpm/[email protected]/node_modules/ms/index.js”) ``` Since this includes the version number, when the package is updated, we ended up with a diff on the serialized code. Even worse, if a package is updated, and you run a pulumi operation that doesn’t run the program (refresh, destroy), you could end up with an error because a dynamic provider would fail to load some of its dependencies. To fix this, we detect these paths and capture for example `require("ms/index.js”)`, the same as under npm or yarn. When updating the `@pulumi/pulumi` SDK to a version with this fix, users will see one final diff that changes the `require` calls. Fixes #10009 Fixes #9085 Partial fix for #18976 (pnpm, not the for packages in the `.aspect_rules_js` folder, which I think is Bazel?)
1 parent c9aaa47 commit 54b4e80

5 files changed

Lines changed: 87 additions & 8 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
component: sdk/nodejs
2+
kind: fix
3+
body: Don't include the version in module paths when serializing functions when using pnpm
4+
time: 2026-07-01T15:18:43.430936+02:00

‎sdk/nodejs/runtime/closure/createClosure.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,15 @@ async function getOrCreateEntryAsync(
13891389
const moduleParts = normalizedModuleName.split("/");
13901390

13911391
const nodeModulesSegment = "node_modules";
1392-
const nodeModulesSegmentIndex = moduleParts.findIndex((v) => v === nodeModulesSegment);
1392+
// pnpm installs packages into a store keyed with version numbers
1393+
// `node_modules/.pnpm/<pkg>@<version>/node_modules/<pkg>/...`, and adds a symlink to this in
1394+
// `node_modules/<pkg>` so Node.js can resolve the package. We see the real path here, not the symlink. In that
1395+
// layout the real package name follows the *last* `node_modules` segment. For npm/yarn (and pnpm's hoisted
1396+
// node-linker) the package name follows the *first* `node_modules` segment.
1397+
const isPnpmStore = moduleParts.includes(".pnpm");
1398+
const nodeModulesSegmentIndex = isPnpmStore
1399+
? moduleParts.lastIndexOf(nodeModulesSegment)
1400+
: moduleParts.indexOf(nodeModulesSegment);
13931401
const isInNodeModules = nodeModulesSegmentIndex >= 0;
13941402

13951403
const isLocalModule = normalizedModuleName.startsWith(".") && !isInNodeModules;

‎sdk/nodejs/runtime/closure/package.ts‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,26 @@ function getPackageDefinition(path: string): PackageDefinition | undefined {
4646
if (last === undefined || lastFullPath === undefined) {
4747
throw new Error(`no package.json found for ${path}`);
4848
}
49-
const packageDefinitionAbsPath = lastFullPath.slice(0, lastFullPath.indexOf(last)) + last + "/package.json";
49+
// Locate the package's directory within the resolved path so we can read its package.json. `last` is the bare
50+
// package specifier (e.g. "lodash" or "@scope/pkg") and its installed directory is `node_modules/<last>`.
51+
//
52+
// We anchor on `node_modules/<last>` rather than the first occurrence of `last` because:
53+
//
54+
// - under pnpm the path is
55+
// `.../node_modules/.pnpm/<name>@<version>/node_modules/<name>/...`,
56+
// - some packages have a main file whose name repeats the package name
57+
// (e.g. lodash resolves to `.../node_modules/lodash/lodash.js`).
58+
// The last occurrence of the `node_modules/<last>` marker is the package's real directory.
59+
const resolvedPath = upath.normalize(lastFullPath);
60+
const marker = `node_modules/${last}`;
61+
const markerIndex = resolvedPath.lastIndexOf(marker);
62+
const packageDefinitionAbsPath =
63+
markerIndex >= 0
64+
? resolvedPath.slice(0, markerIndex + marker.length) + "/package.json"
65+
: // For packages resolved outside of node_modules (e.g. `yarn link`ed or workspace
66+
// packages) there is no marker to anchor on, so fall back to the first occurrence of
67+
// the name in the resolved path.
68+
resolvedPath.slice(0, resolvedPath.indexOf(last)) + last + "/package.json";
5069
return require(packageDefinitionAbsPath);
5170
} catch (err) {
5271
return undefined;

‎sdk/nodejs/tests/runtime/closure-integration-tests.ts‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ async function writePackageJSON(
3737
"@types/mocha": "^10.0.0",
3838
"@types/node": nodeTypesVersion,
3939
"@types/semver": "^7.5.6",
40+
execa: "^5.1.0",
4041
mocha: "^11.0.0",
4142
"mocha-suppress-logs": "^0.5.1",
4243
mockpackage: "file:mockpackage-src",
@@ -47,6 +48,11 @@ async function writePackageJSON(
4748
overrides: {
4849
typescript: typescriptVersion,
4950
},
51+
pnpm: {
52+
overrides: {
53+
typescript: typescriptVersion,
54+
},
55+
},
5056
};
5157

5258
await fs.writeFile(path.join(dir, "package.json"), JSON.stringify(packageJSON, undefined, 4));
@@ -70,7 +76,29 @@ async function copyDir(src: string, dest: string) {
7076
}
7177
}
7278

73-
async function run(typescriptVersion: string, nodeTypesVersion: string) {
79+
// The package managers we run the closure tests against. npm and pnpm lay out
80+
// node_modules differently (pnpm uses a symlinked virtual store under
81+
// node_modules/.pnpm), so we exercise both to ensure function serialization
82+
// resolves module names correctly regardless of the on-disk layout.
83+
type PackageManager = "npm" | "pnpm";
84+
85+
async function install(packageManager: PackageManager, dir: string) {
86+
if (packageManager === "pnpm") {
87+
// Force the isolated node-linker so we actually exercise the
88+
// node_modules/.pnpm/<pkg>@<version>/node_modules/<pkg> layout, even if
89+
// the environment defaults to a hoisted layout.
90+
await fs.writeFile(path.join(dir, ".npmrc"), "node-linker=isolated\n");
91+
// Use pnpm 10, we don't support 11 yet https://github.com/pulumi/pulumi/issues/22893
92+
await execa("corepack", ["use", "pnpm@^10.0.0"], {
93+
cwd: dir,
94+
env: { COREPACK_ENABLE_DOWNLOAD_PROMPT: "0" },
95+
});
96+
} else {
97+
await execa("npm", ["install", "--install-links"], { cwd: dir });
98+
}
99+
}
100+
101+
async function run(packageManager: PackageManager, typescriptVersion: string, nodeTypesVersion: string) {
74102
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), "closure-test-"));
75103
const sdkRoot = path.join(__dirname, "..", "..", "..");
76104
const sdkRootBin = path.join(sdkRoot, "bin");
@@ -81,13 +109,14 @@ async function run(typescriptVersion: string, nodeTypesVersion: string) {
81109
await writePackageJSON(tmpDir, pulumiPackagePath, typescriptVersion, nodeTypesVersion);
82110
await copyDir(path.join(sdkRoot, "tests", "runtime", "testdata", "closure-tests"), tmpDir);
83111

84-
await execa("npm", ["install", "--install-links"], { cwd: tmpDir });
112+
await install(packageManager, tmpDir);
85113

86114
await execa("npx", ["--no-install", "tsc"], { cwd: tmpDir });
87115

88116
await execa("npx", ["--no-install", "mocha", "--timeout", "30000", "test.js"], {
89117
cwd: tmpDir,
90118
stdio: "inherit",
119+
env: { CLOSURE_TEST_PACKAGE_MANAGER: packageManager, COREPACK_ENABLE_DOWNLOAD_PROMPT: "0" },
91120
});
92121

93122
await fs.rm(tmpDir, { recursive: true, force: true });
@@ -101,8 +130,13 @@ async function main() {
101130
["<5.2.0", "ts5.1"], // Awaiter changed slightly in 5.2.0 https://github.com/microsoft/TypeScript/pull/56296
102131
["^5.2.0", "latest"], // Latest 5.x.x
103132
]) {
104-
await run(ts, typesNode);
133+
await run("npm", ts, typesNode);
105134
}
135+
136+
// Also run the suite under pnpm for the default TypeScript version. The module-resolution logic that pnpm exercises
137+
// is independent of the TypeScript version, so a single run is enough to guard against regressions in pnpm's
138+
// symlinked node_modules layout.
139+
await run("pnpm", "~3.8.3", "ts3.8");
106140
}
107141

108142
main().catch((error) => {

‎sdk/nodejs/tests/runtime/testdata/closure-tests/test.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,28 @@ async function getSnapshot(testCase: string, typescriptVersion: string): Promise
4545
return fs.readFile(path.join("cases", testCase, "snapshot.txt"), "utf-8");
4646
}
4747

48+
// Returns the typescript version resolved within the @pulumi/pulumi package, as reported by the package manager.
49+
async function resolvedTypescriptVersion(packageManager: string): Promise<string> {
50+
if (packageManager === "pnpm") {
51+
const { stdout } = await execa("pnpm", ["ls", "typescript", "--json", "--depth", "Infinity"], {
52+
cwd: __dirname,
53+
reject: false,
54+
});
55+
const projects = JSON.parse(stdout);
56+
return projects[0].dependencies["@pulumi/pulumi"].dependencies.typescript.version;
57+
}
58+
const { stdout } = await execa("npm", ["ls", "typescript", "--json"], { cwd: __dirname, reject: false });
59+
const deps = JSON.parse(stdout);
60+
return deps.dependencies["@pulumi/pulumi"].dependencies.typescript.version;
61+
}
62+
4863
// This test validates that the typescript version used by the closure tests
4964
// is the same as the one used by the pulumi package and that we are testing
5065
// what we think we are testing ...
5166
it(`resolve to the correct typescript version within the pulumi package`,
5267
async function () {
53-
const { stdout } = await execa("npm", ["ls", "typescript", "--json"], { cwd: __dirname, reject: false });
54-
const deps = JSON.parse(stdout);
55-
const version = deps.dependencies["@pulumi/pulumi"].dependencies.typescript.version;
68+
const packageManager = process.env.CLOSURE_TEST_PACKAGE_MANAGER ?? "npm";
69+
const version = await resolvedTypescriptVersion(packageManager);
5670
assert.strictEqual(version, typescript.version);
5771
});
5872

0 commit comments

Comments
 (0)