Skip to content

Commit 680d1e4

Browse files
Support absolute paths in node-version-file (#1633)
* path.resolve for composite actions * log and documentation update --------- Co-authored-by: mahabaleshwars <[email protected]>
1 parent db0fb3a commit 680d1e4

4 files changed

Lines changed: 39 additions & 3 deletions

File tree

‎__tests__/main.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ describe('main tests', () => {
177177
);
178178
});
179179

180+
afterEach(() => {
181+
jest.restoreAllMocks();
182+
});
183+
180184
each`
181185
contents | expected
182186
${'12'} | ${'12'}
@@ -315,11 +319,39 @@ describe('main tests', () => {
315319

316320
// Assert
317321
expect(getNodeVersionFromFileSpy).toHaveBeenCalled();
322+
// A relative input is still resolved against the workspace.
323+
expect(getNodeVersionFromFileSpy).toHaveBeenCalledWith(
324+
path.join(process.env['GITHUB_WORKSPACE']!, '.nvmrc')
325+
);
318326
expect(infoSpy).toHaveBeenCalledWith(
319327
`Resolved ${inputs['node-version-file']} as ${expectedVersionSpec}`
320328
);
321329
}, 10000);
322330

331+
it('reads node-version-file given as an absolute path outside the workspace', async () => {
332+
// Arrange: a composite action passes `${{ github.action_path }}/.nvmrc`,
333+
// which is absolute and may sit outside GITHUB_WORKSPACE.
334+
// The workspace deliberately points at a different existing directory,
335+
// so the absolute version file lies outside it.
336+
const workspace = path.join(__dirname, 'mock');
337+
process.env['GITHUB_WORKSPACE'] = workspace;
338+
const versionFilePath = path.join(__dirname, 'data', '.nvmrc');
339+
// Guard the premise: the version file lives outside the workspace.
340+
expect(path.relative(workspace, versionFilePath).startsWith('..')).toBe(
341+
true
342+
);
343+
inputs['node-version-file'] = versionFilePath;
344+
345+
// Act
346+
await main.run();
347+
348+
// Assert: the path is used as provided and the real file is read.
349+
// The expected `24` comes from `__tests__/data/.nvmrc` (`v24`).
350+
expect(getNodeVersionFromFileSpy).toHaveBeenCalledWith(versionFilePath);
351+
expect(infoSpy).toHaveBeenCalledWith(`Resolved ${versionFilePath} as 24`);
352+
expect(core.setFailed as jest.Mock).not.toHaveBeenCalled();
353+
}, 10000);
354+
323355
it('should throw an error if node-version-file is not accessible', async () => {
324356
// Arrange
325357
inputs['node-version-file'] = 'non-existing-file';

‎dist/setup/index.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101351,7 +101351,9 @@ function resolveVersionInput() {
101351101351
return version;
101352101352
}
101353101353
if (versionFileInput) {
101354-
const versionFilePath = external_path_.join(process.env.GITHUB_WORKSPACE, versionFileInput);
101354+
// `path.resolve` (unlike `path.join`) keeps an already-absolute input as-is,
101355+
// so a composite action can pass `${{ github.action_path }}/.nvmrc`.
101356+
const versionFilePath = external_path_.resolve(process.env.GITHUB_WORKSPACE, versionFileInput);
101355101357
const parsedVersion = getNodeVersionFromFile(versionFilePath);
101356101358
if (parsedVersion) {
101357101359
version = parsedVersion;

‎docs/advanced-usage.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ steps:
7979
The `node-version-file` input accepts a path to a file containing the version of Node.js to be used by a project, for example `.nvmrc`, `.node-version`, `.tool-versions`, `mise.toml`, or `package.json`. If both the `node-version` and the `node-version-file` inputs are provided then the `node-version` input is used.
8080
See [supported version syntax](https://github.com/actions/setup-node#supported-version-syntax).
8181

82-
> The action will search for the node version file relative to the repository root.
82+
> The action resolves a relative `node-version-file` path against `GITHUB_WORKSPACE`, which is the repository root by default. If the input is a full absolute path, the action uses that path directly instead of appending it to the workspace.
8383

8484
```yaml
8585
steps:

‎src/main.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ function resolveVersionInput(): string {
120120
}
121121

122122
if (versionFileInput) {
123-
const versionFilePath = path.join(
123+
// `path.resolve` (unlike `path.join`) keeps an already-absolute input as-is,
124+
// so a composite action can pass `${{ github.action_path }}/.nvmrc`.
125+
const versionFilePath = path.resolve(
124126
process.env.GITHUB_WORKSPACE!,
125127
versionFileInput
126128
);

0 commit comments

Comments
 (0)