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(ext/node): add the missing path information when the file is not found in fs.stat and fs.statSync #26037

Merged
merged 7 commits into from
Oct 27, 2024

Conversation

familyboat
Copy link
Contributor

@familyboat familyboat commented Oct 5, 2024

Fix #25921.
See more details in #25921.

familyboat and others added 3 commits October 4, 2024 23:11
Signed-off-by: familyboat <84062528+familyboat@users.noreply.github.com>
Signed-off-by: familyboat <84062528+familyboat@users.noreply.github.com>
@CLAassistant
Copy link

CLAassistant commented Oct 5, 2024

CLA assistant check
All committers have signed the CLA.

@familyboat familyboat marked this pull request as draft October 5, 2024 06:02
@familyboat
Copy link
Contributor Author

I test it with

// main.js
import fs from "node:fs";

const file = "non-exist-file";
const fileUrl = new URL(file, import.meta.url);
const nonFileUrl = new URL(file, "https://example.com");

const files = [file, fileUrl, nonFileUrl];

function testStatSync() {
  files.forEach((item) => {
    try {
      fs.statSync(item);
    } catch (error) {
      console.log(`Error occurred in fs.statSync: ${error}`);
    }

    try {
      Deno.statSync(item);
    } catch (error) {
      console.log(`Error occurred in Deno.statSync: ${error}\n`);
    }
  });
}

async function testStat() {
  for (const item of files) {
    await fs.stat(item, (error) => {
      console.log(`Error occurred in fs.stat: ${error}`);
    });

    try {
      await Deno.stat(item);
    } catch (error) {
      console.log(`Error occurred in Deno.stat: ${error}\n`);
    }
  }
}

testStatSync();
testStat();

Run it with deno run -A main.js
The output is:

Error occurred in fs.statSync: Error: ENOENT: no such file or directory, stat 'non-exist-file'
Error occurred in Deno.statSync: NotFound: No such file or directory (os error 2): stat 'non-exist-file'

Error occurred in fs.statSync: Error: ENOENT: no such file or directory, stat '/root/test/node-file-error-message/non-exist-file'
Error occurred in Deno.statSync: NotFound: No such file or directory (os error 2): stat '/root/test/node-file-error-message/non-exist-file'

Error occurred in fs.statSync: TypeError: Must be a file URL.
Error occurred in Deno.statSync: TypeError: Must be a file URL.

Error occurred in fs.stat: Error: ENOENT: no such file or directory, stat 'non-exist-file'
Error occurred in Deno.stat: NotFound: No such file or directory (os error 2): stat 'non-exist-file'

Error occurred in fs.stat: Error: ENOENT: no such file or directory, stat '/root/test/node-file-error-message/non-exist-file'
Error occurred in Deno.stat: NotFound: No such file or directory (os error 2): stat '/root/test/node-file-error-message/non-exist-file'

Error occurred in fs.stat: TypeError: Must be a file URL.
Error occurred in Deno.stat: TypeError: Must be a file URL.

Running cargo test took several hours, so I gave up.

@familyboat familyboat marked this pull request as ready for review October 5, 2024 13:01
@littledivy
Copy link
Member

@familyboat You can add your test to tests/unit_node/fs_test.ts and run it with:

target/debug/deno test -A --config tests/config/deno.json tests/unit_node/fs_test.ts

@familyboat
Copy link
Contributor Author

familyboat commented Oct 10, 2024

I add tests for node:fs/statSync and node:fs/promises/stat(which is promisify of node:fs/stat).

Copy link
Member

@littledivy littledivy left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@littledivy littledivy merged commit c314b2d into denoland:main Oct 27, 2024
17 checks passed
@familyboat familyboat deleted the familyboat-patch-1 branch October 28, 2024 18:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

node-compa: better error message
3 participants