Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/fix-win-eperm-rmsync.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes `astro build` failing on Windows with Node.js 25+ with `ERR_INVALID_ARG_VALUE` when clearing the output directory hits a transient `EPERM` error
4 changes: 2 additions & 2 deletions packages/astro/src/core/fs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
const fixWinEPERMSync = (p: string, options: fs.RmDirOptions, er: any) => {
const fixWinEPERMSync = (p: string, options: fs.RmOptions, er: any) => {
try {
fs.chmodSync(p, 0o666);
} catch (er2: any) {
Expand All @@ -86,7 +86,7 @@ const fixWinEPERMSync = (p: string, options: fs.RmDirOptions, er: any) => {
}

if (stats.isDirectory()) {
fs.rmdirSync(p, options);
fs.rmSync(p, options);
} else {
fs.unlinkSync(p);
}
Expand Down
57 changes: 57 additions & 0 deletions packages/astro/test/units/fs/empty-dir.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import * as assert from 'node:assert/strict';
import fs from 'node:fs';
import { after, before, describe, it, mock } from 'node:test';
import { pathToFileURL } from 'node:url';
import { createFixture } from '../test-utils.ts';

describe('emptyDir', () => {
const originalPlatform = process.platform;

before(() => {
Object.defineProperty(process, 'platform', { value: 'win32' });
});

after(() => {
Object.defineProperty(process, 'platform', { value: originalPlatform });
mock.restoreAll();
});

it('removes a directory recursively when fs.rmSync fails with EPERM on Windows', async () => {
// `isWindows` is evaluated at module load, so import a fresh copy while `process.platform` is faked.
const { emptyDir } = await import(`../../../dist/core/fs/index.js?win32=${Date.now()}`);

const fixture = await createFixture({
'/dist/_astro/nested/file.txt': 'x',
'/dist/index.html': 'x',
});
const distDir = pathToFileURL(`${fixture.getPath('dist')}/`);

const realRmSync = fs.rmSync;
let failed = false;
mock.method(fs, 'rmSync', (p: fs.PathLike, options?: fs.RmOptions) => {
if (!failed) {
failed = true;
throw Object.assign(new Error('EPERM: operation not permitted'), { code: 'EPERM' });
}
return realRmSync(p, options);
});
// On POSIX, `chmodSync(dir, 0o666)` drops the execute bit and makes the directory untraversable;
// on Windows it only toggles the read-only flag, so skip it to mirror Windows behavior.
mock.method(fs, 'chmodSync', () => {});
// Node.js 25+ throws when `fs.rmdirSync` receives `recursive`.
const realRmdirSync = fs.rmdirSync;
mock.method(fs, 'rmdirSync', (p: fs.PathLike, options?: { recursive?: boolean }) => {
if (options && 'recursive' in options) {
throw new TypeError("The property 'options.recursive' is no longer supported.");
}
return realRmdirSync(p);
});

emptyDir(distDir);

assert.equal(failed, true);
assert.deepEqual(fs.readdirSync(distDir), []);

await fixture.rm();
});
});
Loading