Skip to content
Open
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
6 changes: 3 additions & 3 deletions sources/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export class Engine {
* project using the default package managers, and configure it so that we
* don't need to ask again in the future.
*/
async findProjectSpec(initialCwd: string, locator: Locator | LazyLocator, {transparent = false}: {transparent?: boolean} = {}): Promise<Descriptor> {
async findProjectSpec(initialCwd: string, locator: Locator | LazyLocator, {transparent = false, binaryVersion}: {transparent?: boolean, binaryVersion?: string | null} = {}): Promise<Descriptor> {
// A locator is a valid descriptor (but not the other way around)
const fallbackDescriptor = {name: locator.name, range: `${locator.reference}`};

Expand Down Expand Up @@ -293,7 +293,7 @@ export class Engine {
}

case `Found`: {
const spec = result.getSpec();
const spec = result.getSpec({enforceExactVersion: !binaryVersion});
if (spec.name !== locator.name) {
if (transparent) {
if (typeof locator.reference === `function`)
Expand Down Expand Up @@ -344,7 +344,7 @@ export class Engine {
};
}

const descriptor = await this.findProjectSpec(cwd, fallbackLocator, {transparent: isTransparentCommand});
const descriptor = await this.findProjectSpec(cwd, fallbackLocator, {transparent: isTransparentCommand, binaryVersion});

if (binaryVersion)
descriptor.range = binaryVersion;
Expand Down
4 changes: 2 additions & 2 deletions sources/specUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export async function setLocalPackageManager(cwd: string, info: PreparedPackageM
interface FoundSpecResult {
type: `Found`;
target: string;
getSpec: () => Descriptor;
getSpec: (options?: {enforceExactVersion?: boolean}) => Descriptor;
range?: Descriptor & {onFail?: DevEngineDependency[`onFail`]};
envFilePath?: string;
}
Expand Down Expand Up @@ -249,6 +249,6 @@ export async function loadSpec(initialCwd: string): Promise<LoadSpecResult> {
onFail: selection.data.devEngines.packageManager.onFail,
},
// Lazy-loading it so we do not throw errors on commands that do not need valid spec.
getSpec: () => parseSpec(rawPmSpec, path.relative(initialCwd, selection.manifestPath)),
getSpec: ({enforceExactVersion = true} = {}) => parseSpec(rawPmSpec, path.relative(initialCwd, selection.manifestPath), {enforceExactVersion}),
};
}
48 changes: 48 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1709,3 +1709,51 @@ describe(`handle integrity checks`, () => {
});
});
});

describe(`allow range versions in devEngines.packageManager.version when user specifies exact version`, () => {
for (const {name, versionRange, userProvidedVersion} of [
{name: `npm`, versionRange: `^10.7.0`, userProvidedVersion: `6.14.2`},
{name: `yarn`, versionRange: `^2.2.0`, userProvidedVersion: `2.2.2`},
{name: `pnpm`, versionRange: `^5.8.0`, userProvidedVersion: `5.8.0`},
]) {
it(`should work with ${name}`, async () => {
await xfs.mktempPromise(async cwd => {
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
devEngines: {
packageManager: {
name,
version: versionRange,
},
},
});

await expect(runCli(cwd, [`${name}@${userProvidedVersion}`, `--version`])).resolves.toMatchObject({
exitCode: 0,
stderr: ``,
stdout: `${userProvidedVersion}\n`,
});
});
});
}
});

it(`should still validate devEngines.packageManager.version format when no user version specified`, async () => {
await xfs.mktempPromise(async cwd => {
// When no user version is specified, range versions in devEngines should still cause error
await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), {
devEngines: {
packageManager: {
name: `npm`,
version: `^6.14.2`,
},
},
});

// Without user-specified version, should still fail due to range version in devEngines
await expect(runCli(cwd, [`npm`, `--version`])).resolves.toMatchObject({
exitCode: 1,
stderr: expect.stringContaining(`Invalid package manager specification in package.json (npm@^6.14.2); expected a semver version`),
stdout: ``,
});
});
});