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

fix(node/fs): chmod function throws unnecessary TypeError on Windows #3168

Merged
merged 2 commits into from
Feb 8, 2023
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
26 changes: 24 additions & 2 deletions node/_fs/_fs_chmod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ export function chmod(
callback: CallbackWithError,
) {
path = getValidatedPath(path).toString();
mode = parseFileMode(mode, "mode");

try {
mode = parseFileMode(mode, "mode");
} catch (error) {
// TODO(PolarETech): Errors should not be ignored when Deno.chmod is supported on Windows.
// https://github.com/denoland/deno_std/issues/2916
if (Deno.build.os === "windows") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: How about branching by this condition before doing try block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then how about the following?

  path = getValidatedPath(path).toString();

  if (Deno.build.os === "windows" && mode === null) {
    mode = 0;
  }
  mode = parseFileMode(mode, "mode");

  Deno.chmod(pathModule.toNamespacedPath(path), mode).catch((error) => {

I realized that we might need to consider the case where Deno.chmod supports Windows in the future, and I assume that if Deno.chmod works, then parseFileMode must be run.
It could be the case that Deno.chmod is supported on Windows but the code here is left as is.
To prepare for such a case, I thought parseFileMode would run, and if an error occurred, setting mode to a dummy value (0 would be safest) would solve the current issue.

However, the concern remains that if a situation arises in the future where Deno.chmod supports Windows but Deno.FileInfo.mode does not, the permissions will be set to 0 unexpectedly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation. Now the current code just makes sense to me.

mode = 0; // set dummy value to avoid type checking error at Deno.chmod
} else {
throw error;
}
}

Deno.chmod(pathModule.toNamespacedPath(path), mode).catch((error) => {
// Ignore NotSupportedError that occurs on windows
Expand All @@ -33,7 +44,18 @@ export const chmodPromise = promisify(chmod) as (

export function chmodSync(path: string | URL, mode: string | number) {
path = getValidatedPath(path).toString();
mode = parseFileMode(mode, "mode");

try {
mode = parseFileMode(mode, "mode");
} catch (error) {
// TODO(PolarETech): Errors should not be ignored when Deno.chmodSync is supported on Windows.
// https://github.com/denoland/deno_std/issues/2916
if (Deno.build.os === "windows") {
mode = 0; // set dummy value to avoid type checking error at Deno.chmodSync
} else {
throw error;
}
}

try {
Deno.chmodSync(pathModule.toNamespacedPath(path), mode);
Expand Down
31 changes: 31 additions & 0 deletions node/_fs/_fs_chmod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ Deno.test({
},
});

Deno.test({
name: "ASYNC: don't throw errors for mode parameter (Windows)",
ignore: !isWindows,
async fn() {
const tempFile: string = await Deno.makeTempFile();
await new Promise<void>((resolve, reject) => {
// @ts-ignore for test
chmod(tempFile, null, (err) => {
if (err) reject(err);
else resolve();
});
}).finally(() => {
Deno.removeSync(tempFile);
});
},
});

Deno.test({
name: "ASYNC: don't throw NotSupportedError (Windows)",
ignore: !isWindows,
Expand Down Expand Up @@ -83,6 +100,20 @@ Deno.test({
},
});

Deno.test({
name: "SYNC: don't throw errors for mode parameter (Windows)",
ignore: !isWindows,
fn() {
const tempFile: string = Deno.makeTempFileSync();
try {
// @ts-ignore for test
chmodSync(tempFile, null);
} finally {
Deno.removeSync(tempFile);
}
},
});

Deno.test({
name: "SYNC: don't throw NotSupportedError (Windows)",
ignore: !isWindows,
Expand Down