Skip to content

fix(@ngtools/webpack): support locating PNPM lock file during NGCC processing #22633

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

Merged
merged 1 commit into from
Feb 7, 2022
Merged
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
32 changes: 21 additions & 11 deletions packages/ngtools/webpack/src/ngcc_processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,6 @@ export class NgccProcessor {
const runHashBasePath = path.join(this._nodeModulesDirectory, '.cli-ngcc');
const projectBasePath = path.join(this._nodeModulesDirectory, '..');
try {
let lockData;
let lockFile = 'yarn.lock';
try {
lockData = readFileSync(path.join(projectBasePath, lockFile));
} catch {
lockFile = 'package-lock.json';
lockData = readFileSync(path.join(projectBasePath, lockFile));
}

let ngccConfigData;
try {
ngccConfigData = readFileSync(path.join(projectBasePath, 'ngcc.config.js'));
Expand All @@ -91,11 +82,12 @@ export class NgccProcessor {

const relativeTsconfigPath = path.relative(projectBasePath, this.tsConfigPath);
const tsconfigData = readFileSync(this.tsConfigPath);
const { lockFileData, lockFilePath } = this.findPackageManagerLockFile(projectBasePath);

// Generate a hash that represents the state of the package lock file and used tsconfig
const runHash = createHash('sha256')
.update(lockData)
.update(lockFile)
.update(lockFileData)
.update(lockFilePath)
.update(ngccConfigData)
.update(tsconfigData)
.update(relativeTsconfigPath)
Expand Down Expand Up @@ -248,6 +240,24 @@ export class NgccProcessor {

throw new Error(`Cannot locate the 'node_modules' directory.`);
}

private findPackageManagerLockFile(projectBasePath: string): {
lockFilePath: string;
lockFileData: Buffer;
} {
for (const lockFile of ['yarn.lock', 'pnpm-lock.yaml', 'package-lock.json']) {
const lockFilePath = path.join(projectBasePath, lockFile);

try {
return {
lockFilePath,
lockFileData: readFileSync(lockFilePath),
};
} catch {}
}

throw new Error('Cannot locate a package manager lock file.');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we list the files we tried in this error message? Otherwise it's hard to know if the CLI is looking in the wrong place or if the file itself was misplaced.

Copy link
Collaborator Author

@alan-agius4 alan-agius4 Feb 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this error message is never shown to the users. As this is non fatal and the exception is caught at a later stage.

// Any error means an ngcc execution is needed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Up to you, could be useful for local development if it's ever thrown for an unexpected reason.

}
}

class NgccLogger implements Logger {
Expand Down