Skip to content

Commit 77793c7

Browse files
authored
Merge pull request microsoft#610 from Microsoft/pgonzal/rush-mac-recycler
[rush] Fix issue with cleaning "rush-recycler" on macOS
2 parents 2decbc9 + 9ecb73f commit 77793c7

2 files changed

Lines changed: 49 additions & 18 deletions

File tree

apps/rush-lib/src/utilities/AsyncRecycler.ts

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,33 +95,53 @@ export default class AsyncRecycler {
9595
}
9696

9797
// Asynchronously delete the folder contents.
98-
const recyclerFolderContents: string = path.join(this.recyclerFolder, '*');
99-
100-
const windowsTrimmedRecyclerFolder: string = this.recyclerFolder.match(/\\$/)
101-
? this.recyclerFolder.substring(0, this.recyclerFolder.length - 1)
102-
: this.recyclerFolder;
103-
const command: string = os.platform() === 'win32'
104-
// Windows
105-
? 'cmd.exe'
106-
// Assume 'NIX or Darwin
107-
: 'rm';
108-
109-
const args: string[] = os.platform() === 'win32'
110-
// Windows
111-
? [
98+
let command: string;
99+
let args: string[];
100+
101+
if (os.platform() === 'win32') {
102+
const recyclerFolderWildcard: string = path.join(this.recyclerFolder, '*');
103+
104+
const windowsTrimmedRecyclerFolder: string = this.recyclerFolder.match(/\\$/)
105+
? this.recyclerFolder.substring(0, this.recyclerFolder.length - 1)
106+
: this.recyclerFolder;
107+
command = 'cmd.exe';
108+
109+
args = [
112110
'/c',
113-
`FOR /F %f IN ('dir /B \\\\?\\${recyclerFolderContents}') `
111+
`FOR /F %f IN ('dir /B \\\\?\\${recyclerFolderWildcard}') `
114112
+ `DO rd /S /Q \\\\?\\${windowsTrimmedRecyclerFolder}\\%f`
115-
]
116-
// Assume 'NIX or Darwin
117-
: ['-rf', `"${recyclerFolderContents}"`];
113+
];
114+
} else {
115+
command = 'rm';
116+
args = [ '-rf' ];
117+
118+
let pathCount: number = 0;
119+
120+
// child_process.spawn() doesn't expand wildcards. To be safe, we will do it manually
121+
// rather than rely on an unknown shell.
122+
for (const filename of fsx.readdirSync(this.recyclerFolder)) {
123+
// The "." and ".." are supposed to be excluded, but let's be safe
124+
if (filename !== '.' && filename !== '..') {
125+
args.push(path.join(this.recyclerFolder, filename));
126+
++pathCount;
127+
}
128+
}
129+
130+
if (pathCount === 0) {
131+
// Nothing to do
132+
return;
133+
}
134+
}
118135

119136
const options: child_process.SpawnOptions = {
120137
detached: true,
138+
// The child won't stay alive unless we detach its stdio
121139
stdio: [ 'ignore', 'ignore', 'ignore' ]
122140
};
123141

124142
const process: child_process.ChildProcess = child_process.spawn(command, args, options);
143+
144+
// The child won't stay alive unless we unlink it from the parent process
125145
process.unref();
126146
}
127147

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Fix a problem where the \"rush-recycler\" folder was not getting cleaned on macOS",
5+
"packageName": "@microsoft/rush",
6+
"type": "none"
7+
}
8+
],
9+
"packageName": "@microsoft/rush",
10+
"email": "pgonzal@users.noreply.github.com"
11+
}

0 commit comments

Comments
 (0)