Skip to content

Commit

Permalink
Prevent crash while reading non-existent file on GITHUB_EVENT_PATH (#…
Browse files Browse the repository at this point in the history
…5835)

**What's the problem this PR addresses?**
<!-- Describe the rationale of your PR. -->
<!-- Link all issues that it closes. (Closes/Resolves #xxxx.) -->

While building projects in Docker/VM on GitHub actions, the envs are
present, but some files are not. Like:
https://github.com/Brooooooklyn/keyring-node/actions/runs/6622119637/job/17987136808?pr=25

**How did you fix it?**
<!-- A detailed description of your implementation. -->

Try catch while reading the file path.

**Checklist**
<!--- Don't worry if you miss something, chores are automatically
tested. -->
<!--- This checklist exists to help you remember doing the chores when
you submit a PR. -->
<!--- Put an `x` in all the boxes that apply. -->
- [x] I have read the [Contributing
Guide](https://yarnpkg.com/advanced/contributing).

<!-- See
https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released
for more details. -->
<!-- Check with `yarn version check` and fix with `yarn version check
-i` -->
- [x] I have set the packages that need to be released for my changes to
be effective.

<!-- The "Testing chores" workflow validates that your PR follows our
guidelines. -->
<!-- If it doesn't pass, click on it to see details as to what your PR
might be missing. -->
- [x] I will check that all automated PR checks pass before the PR gets
reviewed.

---------

Co-authored-by: Maël Nison <[email protected]>
  • Loading branch information
Brooooooklyn and arcanis authored Oct 24, 2023
1 parent 8b92696 commit 3823259
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 4 deletions.
34 changes: 34 additions & 0 deletions .yarn/versions/d7044db8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/core": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-http"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/doctor"
- "@yarnpkg/extensions"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ describe(`Commands`, () => {
}),
);


test(
`it should not enable --refresh-lockfile --immutable in private PR CIs`,
makeTemporaryEnv({
Expand Down Expand Up @@ -221,6 +220,65 @@ describe(`Commands`, () => {
}),
);

test(
`it should not enable --refresh-lockfile --immutable if the GH environment file is missing`,
makeTemporaryEnv({
dependencies: {
[`one-fixed-dep`]: `1.0.0`,
},
}, async ({path, run, source}) => {
await run(`install`);

const lockfilePath = ppath.join(path, Filename.lockfile);
const lockfileContent = await xfs.readFilePromise(lockfilePath, `utf8`);
const modifiedLockfile = lockfileContent.replace(/no-deps: "npm:1.0.0"/, `no-deps: "npm:2.0.0"`);
await xfs.writeFilePromise(lockfilePath, modifiedLockfile);

const eventPath = ppath.join(path, `github-event-file.json`);

await run(`install`);

await run(`install`, {
env: {
GITHUB_ACTIONS: `true`,
GITHUB_EVENT_NAME: `pull_request`,
GITHUB_EVENT_PATH: npath.fromPortablePath(eventPath),
},
});
}),
);

test(
`it should not enable --refresh-lockfile --immutable if the GH environment file is weird`,
makeTemporaryEnv({
dependencies: {
[`one-fixed-dep`]: `1.0.0`,
},
}, async ({path, run, source}) => {
await run(`install`);

const lockfilePath = ppath.join(path, Filename.lockfile);
const lockfileContent = await xfs.readFilePromise(lockfilePath, `utf8`);
const modifiedLockfile = lockfileContent.replace(/no-deps: "npm:1.0.0"/, `no-deps: "npm:2.0.0"`);
await xfs.writeFilePromise(lockfilePath, modifiedLockfile);

const eventPath = ppath.join(path, `github-event-file.json`);
await xfs.writeJsonPromise(eventPath, {
hello: `world`,
});

await run(`install`);

await run(`install`, {
env: {
GITHUB_ACTIONS: `true`,
GITHUB_EVENT_NAME: `pull_request`,
GITHUB_EVENT_PATH: npath.fromPortablePath(eventPath),
},
});
}),
);

test(
`it should accept to add files to the cache when using --immutable without --immutable-cache`,
makeTemporaryEnv({
Expand Down
15 changes: 12 additions & 3 deletions packages/yarnpkg-core/sources/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,18 @@ import * as semverUtils
import * as structUtils from './structUtils';
import {IdentHash, Package, Descriptor, PackageExtension, PackageExtensionType, PackageExtensionStatus, Locator} from './types';

const isPublicRepository = GITHUB_ACTIONS && process.env.GITHUB_EVENT_PATH
? !(xfs.readJsonSync(npath.toPortablePath(process.env.GITHUB_EVENT_PATH)).repository?.private ?? true)
: false;
const isPublicRepository = (function () {
if (GITHUB_ACTIONS && process.env.GITHUB_EVENT_PATH) {
const githubEventPath = npath.toPortablePath(process.env.GITHUB_EVENT_PATH);
try {
return !(xfs.readJsonSync(githubEventPath).repository?.private ?? true);
} catch (err) {
return false;
}
}

return false;
})();

export const LEGACY_PLUGINS = new Set([
`@yarnpkg/plugin-constraints`,
Expand Down

0 comments on commit 3823259

Please sign in to comment.