Skip to content
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

refactor(@angular/build): use newer Node.js cp API for asset copying #29933

Merged
merged 1 commit into from
Mar 25, 2025
Merged
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion packages/angular/build/src/builders/application/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
import { Result, ResultKind } from './results';
import { Schema as ApplicationBuilderOptions } from './schema';

const isNodeV22orHigher = Number(process.versions.node.split('.', 1)[0]) >= 22;

export type { ApplicationBuilderOptions };

export async function* buildApplicationInternal(
Expand Down Expand Up @@ -211,7 +213,17 @@ export async function* buildApplication(
await fs.writeFile(fullFilePath, file.contents);
} else {
// Copy file contents
await fs.copyFile(file.inputPath, fullFilePath, fs.constants.COPYFILE_FICLONE);
if (isNodeV22orHigher) {
// Use newer `cp` API on Node.js 22+ (minimum v22 for CLI is 22.11)
await fs.cp(file.inputPath, fullFilePath, {
mode: fs.constants.COPYFILE_FICLONE,
preserveTimestamps: true,
});
} else {
// For Node.js 20 use `copyFile` (`cp` is not stable for v20)
// TODO: Remove when Node.js 20 is no longer supported
await fs.copyFile(file.inputPath, fullFilePath, fs.constants.COPYFILE_FICLONE);
}
}
});

Expand Down
14 changes: 14 additions & 0 deletions tests/legacy-cli/e2e/tests/build/assets.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import assert from 'node:assert/strict';
import * as fs from 'node:fs';
import { expectFileToExist, expectFileToMatch, writeFile } from '../../utils/fs';
import { ng } from '../../utils/process';
import { updateJsonFile } from '../../utils/project';
import { expectToFail } from '../../utils/utils';
import { getGlobalVariable } from '../../utils/env';

const isNodeV22orHigher = Number(process.versions.node.split('.', 1)[0]) >= 22;

export default async function () {
await writeFile('public/.file', '');
await writeFile('public/test.abc', 'hello world');
const originalStats = fs.statSync('public/test.abc', { bigint: true });

await ng('build', '--configuration=development');

Expand All @@ -15,6 +20,15 @@ export default async function () {
await expectFileToMatch('dist/test-project/browser/test.abc', 'hello world');
await expectToFail(() => expectFileToExist('dist/test-project/browser/.gitkeep'));

// Timestamp preservation only supported with application build system on Node.js v22+
if (isNodeV22orHigher && getGlobalVariable('argv')['esbuild']) {
const outputStats = fs.statSync('dist/test-project/browser/test.abc', { bigint: true });
assert(
originalStats.mtimeMs === outputStats.mtimeMs,
'Asset file modified timestamp should be preserved.',
);
}

// Ensure `followSymlinks` option follows symlinks
await updateJsonFile('angular.json', (workspaceJson) => {
const appArchitect = workspaceJson.projects['test-project'].architect;
Expand Down