Skip to content

Commit 12305f7

Browse files
clydinhansl
authored andcommitted
fix(@angular/cli): improve resiliency of local package discovery
1 parent 4c4e408 commit 12305f7

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

packages/angular/cli/lib/init.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,28 @@ if (process.env['NG_CLI_PROFILING']) {
5959
process.on('uncaughtException', () => exitHandler({ exit: true }));
6060
}
6161

62+
const isInside = (base: string, potential: string): boolean => {
63+
const absoluteBase = path.resolve(base);
64+
const absolutePotential = path.resolve(potential);
65+
const relativePotential = path.relative(absoluteBase, absolutePotential);
66+
if (!relativePotential.startsWith('..') && !path.isAbsolute(relativePotential)) {
67+
return true;
68+
}
69+
70+
return false;
71+
};
72+
6273
let cli;
6374
try {
64-
const projectLocalCli = require.resolve('@angular/cli', { paths: [ process.cwd() ]});
75+
const projectLocalCli = require.resolve(
76+
'@angular/cli',
77+
{ paths: [ path.join(process.cwd(), 'node_modules'), process.cwd() ]},
78+
);
79+
80+
const isGlobal = isInside(path.join(__dirname, '..'), projectLocalCli);
81+
if (isGlobal) {
82+
throw new Error();
83+
}
6584

6685
// This was run from a global, check local version.
6786
const globalVersion = new SemVer(packageJson['version']);

packages/angular/cli/upgrade/version.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,30 @@ export class Version {
3333
static assertCompatibleAngularVersion(projectRoot: string) {
3434
let angularPkgJson;
3535
let rxjsPkgJson;
36+
37+
const isInside = (base: string, potential: string): boolean => {
38+
const absoluteBase = path.resolve(base);
39+
const absolutePotential = path.resolve(potential);
40+
const relativePotential = path.relative(absoluteBase, absolutePotential);
41+
if (!relativePotential.startsWith('..') && !path.isAbsolute(relativePotential)) {
42+
return true;
43+
}
44+
45+
return false;
46+
};
47+
3648
try {
37-
angularPkgJson = requireProjectModule(projectRoot, '@angular/core/package.json');
38-
rxjsPkgJson = requireProjectModule(projectRoot, 'rxjs/package.json');
49+
const resolveOptions = { paths: [ path.join(projectRoot, 'node_modules'), projectRoot ] };
50+
const angularPackagePath = require.resolve('@angular/core/package.json', resolveOptions);
51+
const rxjsPackagePath = require.resolve('rxjs/package.json', resolveOptions);
52+
53+
if (!isInside(projectRoot, angularPackagePath)
54+
|| !isInside(projectRoot, rxjsPackagePath)) {
55+
throw new Error();
56+
}
57+
58+
angularPkgJson = require(angularPackagePath);
59+
rxjsPkgJson = require(rxjsPackagePath);
3960
} catch {
4061
console.error(terminal.bold(terminal.red(tags.stripIndents`
4162
You seem to not be depending on "@angular/core" and/or "rxjs". This is an error.

0 commit comments

Comments
 (0)