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: skip proc.cwd() when cwd is given #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

scarf005
Copy link

@scarf005 scarf005 commented Mar 6, 2024

Initial checklist

  • I read the support docs
  • I read the contributing guide
  • I agree to follow the code of conduct
  • I searched issues and couldn’t find anything (or linked relevant results below)
  • If applicable, I’ve added docs and tests

Description of changes

make VFile skip unnecessary filesystem access for process.cwd() if cwd is already given.

due to how VFile.cwd works:

  1. set to process.cwd() (/ if browser)
  2. override to options.cwd if exists as stated in Add support for cwd option #15 (comment)

VFile will always request filesystem access even if it's not needed.

When explicitly given `cwd` in constructor, it's unnecessary to request `process.cwd()`.
This allows using this library on capability-based runtimes (e.g Deno and Node experimental) where reading is forbidden.
@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Mar 6, 2024
@wooorm
Copy link
Member

wooorm commented Mar 6, 2024

Heya!

Let me try to fix the build issue.

VFile will always request filesystem access even if it's unnecessary.

This project should be usable. proc redirects to a file that depends on conditions. Only explicitly in Node does one get process.

Why are you/Deno specifically using a node condition?

import_map.json

Right, you specifically choose to use Node APIs. Why?

Also, does Deno expect users to map every imported thing to a file on the file system? These internals specifically contain do-not-use. I was not aware of that. That does not sound good.

@scarf005
Copy link
Author

scarf005 commented Mar 6, 2024

Only explicitly in Node does one get process.

I was using import { VFile } from "npm:vfile@6.0.1" out of habit, and the npm: specifier polyfilled node process.cwd() (Deno.cwd()).

changing import to import { VFile } from "https://esm.sh/vfile@6.0.1", does resolve to browser-based module.

Why are you/Deno specifically using a node condition?

to recap:

  • npm:vfile: node polyfill, for server
  • https://esm.sh/vfile: for browser

I think it makes sense to choose node condition depending on how the library is used. The concern I have right now would be

Right, you specifically choose to use Node APIs. Why?

Also, does Deno expect users to map every imported thing to a file on the file system? These internals specifically contain do-not-use. I was not aware of that. That does not sound good.

Oh, most wouldn't, please ignore this one, I experimented running repo code as-is while reproducing the issue and had to do manual mapping myself.

VFile will always request filesystem access even if it's unnecessary.

This project should be usable.

I'm sorry, could you elaborate further? Would it mean vfile should always request cwd even if it's explicitly given in constructor?

@wooorm
Copy link
Member

wooorm commented Mar 6, 2024

Ah okay, so the import map has to do with trying to make a small repro, good!

I'm sorry, could you elaborate further? Would it mean vfile should always request cwd even if it's explicitly given in constructor?

Nope!

First: a cwd (and path) is really useful. There are many plugins and other things that will look for package.jsons or .git or whatnot relating to the file. path can be relative (in which case it relates to cwd). And cwd (where a programmer runs code from) is also very useful.

But process.cwd is a Node thing. So only if Node is explicitly specified, will process.cwd be used:

vfile/package.json

Lines 36 to 50 in de87fea

"exports": {
".": "./index.js",
"./do-not-use-conditional-minpath": {
"node": "./lib/minpath.js",
"default": "./lib/minpath.browser.js"
},
"./do-not-use-conditional-minproc": {
"node": "./lib/minproc.js",
"default": "./lib/minproc.browser.js"
},
"./do-not-use-conditional-minurl": {
"node": "./lib/minurl.js",
"default": "./lib/minurl.browser.js"
}
},

Everything else gets the default condition. And so, gets:

return '/'

@scarf005
Copy link
Author

scarf005 commented Mar 6, 2024

sorry, I'm getting even more confused. My point was:

  1. it makes sense to ask process.cwd() when cwd is not given in node (new VFile()).
  2. it's needless to ask for process.cwd() when cwd is already given in node (new VFile({ cwd: "/path/to/cwd" })).

@wooorm
Copy link
Member

wooorm commented Mar 6, 2024

Maybe. But:

  • this shouldn’t be something you notice, you are not using Node
  • which cwd value are you setting? cwds are quite useful, perhaps we can improve cwds on deno
  • how are you getting a relevant cwd, perhaps on deno we can use those cwd values you somehow find

@scarf005
Copy link
Author

scarf005 commented Mar 7, 2024

make VFile skip unnecessary filesystem access for process.cwd() if cwd is already given.

I updated the PR description to remove mentions of deno, they got less relevant as i changed my position to optimization (removing unneeded system call)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤞 phase/open Post is being triaged manually
Development

Successfully merging this pull request may close these issues.

None yet

2 participants