Skip to content

Commit 1b4b318

Browse files
mhchenbestander
authored andcommitted
fix(cli): fail when yarn adding Github package with no version (#5292) (#5318)
* fix(cli): fail when `yarn add`ing Github package with no version (#5292) * Attempt to re-trigger AppVeyor build
1 parent 7fcf4dd commit 1b4b318

File tree

7 files changed

+65
-1
lines changed

7 files changed

+65
-1
lines changed

__tests__/commands/add.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@ test.concurrent('install from github', async () => {
114114
await runAdd(['substack/node-mkdirp#master'], {}, 'install-github');
115115
});
116116

117+
test.concurrent('install from github with invalid version should fail', async () => {
118+
let message = '';
119+
try {
120+
await runAdd(['yarnpkg/example-yarn-package#invalid-package-json-version'], {}, 'install-github');
121+
} catch (err) {
122+
message = err.message;
123+
}
124+
expect(message).toEqual(expect.stringContaining('invalid package version'));
125+
});
126+
117127
test.concurrent('install with --dev flag', async () => {
118128
await runAdd(['[email protected]'], {dev: true}, 'add-with-flag', async config => {
119129
const lockfile = explodeLockfile(await fs.readFile(path.join(config.cwd, 'yarn.lock')));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
HTTP/1.1 200 OK
2+
Content-Security-Policy: default-src 'none'; style-src 'unsafe-inline'; sandbox
3+
Strict-Transport-Security: max-age=31536000
4+
X-Content-Type-Options: nosniff
5+
X-Frame-Options: deny
6+
X-XSS-Protection: 1; mode=block
7+
ETag: "d5561550f2f68bb15a0801ccab1c6f5362959c40"
8+
Content-Type: text/plain; charset=utf-8
9+
Cache-Control: max-age=300
10+
X-Geo-Block-List:
11+
X-GitHub-Request-Id: 49EE:29366:1920CD4:1A583B0:5A779863
12+
Content-Length: 463
13+
Accept-Ranges: bytes
14+
Date: Sun, 04 Feb 2018 23:33:57 GMT
15+
Via: 1.1 varnish
16+
Connection: keep-alive
17+
X-Served-By: cache-pao17421-PAO
18+
X-Cache: MISS
19+
X-Cache-Hits: 0
20+
X-Timer: S1517787238.609785,VS0,VE119
21+
Vary: Authorization,Accept-Encoding
22+
Access-Control-Allow-Origin: *
23+
X-Fastly-Request-ID: b02c8764f6d89b30ee060159193da00b51b9fbd8
24+
Expires: Sun, 04 Feb 2018 23:38:57 GMT
25+
Source-Age: 0
26+
27+
{
28+
"name": "example-yarn-package",
29+
"description": "An example package to demonstrate Yarn",
30+
"main": "index.js",
31+
"repository": {
32+
"url": "github.com/yarnpkg/example-yarn-package",
33+
"type": "git"
34+
},
35+
"scripts": {
36+
"test": "jest"
37+
},
38+
"author": "Yarn Contributors",
39+
"license": "BSD-2-Clause",
40+
"dependencies": {
41+
"lodash": "^4.16.2"
42+
},
43+
"devDependencies": {
44+
"jest-cli": "15.1.1"
45+
},
46+
"jest": {
47+
"testEnvironment": "node"
48+
}
49+
}

src/package-request.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@ export default class PackageRequest {
219219
// find version info for this package pattern
220220
const info: Manifest = await this.findVersionInfo();
221221

222+
if (!semver.valid(info.version)) {
223+
throw new MessageError(this.reporter.lang('invalidPackageVersion', info.name, info.version));
224+
}
225+
222226
info.fresh = fresh;
223227
cleanDependencies(info, false, this.reporter, () => {
224228
// swallow warnings

src/reporters/lang/en.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const messages = {
8989
invalidHostedGitFragment: 'Invalid hosted git fragment $0.',
9090
invalidFragment: 'Invalid fragment $0.',
9191
invalidPackageName: 'Invalid package name.',
92+
invalidPackageVersion: "Can't add $0: invalid package version $1.",
9293
couldntFindManifestIn: "Couldn't find manifest in $0.",
9394
shrinkwrapWarning:
9495
'npm-shrinkwrap.json found. This will not be updated or respected. See https://yarnpkg.com/en/docs/migrating-from-npm for more information.',

src/util/git.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ export default class Git implements GitRefResolvingInterface {
473473
});
474474
if (!resolvedResult) {
475475
throw new MessageError(
476-
this.reporter.lang('couldntFindMatch', version, Object.keys(refs).join(','), this.gitUrl.repository),
476+
this.reporter.lang('couldntFindMatch', version, Array.from(refs.keys()).join(','), this.gitUrl.repository),
477477
);
478478
}
479479

0 commit comments

Comments
 (0)