Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
add forked docker process #39
add forked docker process #39
Changes from 1 commit
7a85d69
6124093
6a50c05
18d932a
8db69a9
f6cdc96
8d88bfb
b40c77f
bd153a2
464c8e4
735c443
7d51368
7790ae7
df58b89
b528592
2e5a890
efa8ed1
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather not hardcode this path. Can you
__filename
like in the example code in https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are 3 main ways to get where a file is located. __dirname to get the directory, __filename to get the file, or process.cwd() to get the current working directory of the process. I had to do a little bit of fiddling to get __dirname and __filename to work:
As long as the package is not symlinked it works as expected:
however, if you symlink the package to work locally, it will not work:
The problem is that it points to the path of code that you are symlinking, not the copy in .node_modules (even though it does exist in .node_modules, which is why hard coding worked)
I am not sure how often not being able to symlink is a dealbreaker for local development. I use it, but I don't know if it's widely used by others using the package.
Would you still like me to use __dirname + the file name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added code in the last commit that checks to see if the directory includes node_modules in the path. if it does it just uses __dirname but if it doesn't (which would mean a symlink) then it returns the hard coded path to the node_modules file.
It's not too much cleaner than just hard coding it, but a little better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Take another look at the sample code fork
child_process.fork
in the Node.js documentation:It's the same file. You have some variable --- in this case
process.argv
--- that you use to check if you are in the parent process or the child process.This is analogous to the typical usage of the
fork()
syscall. See example.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what you are saying exactly here.
It sounds like you are giving me advice on how to tell what is the parent vs child process. This is not an issue that I am having.
The issue that you initially pointed out was that the filename was hard coded. You wanted me to use something similar to __filename. (__filename doesn't work out of the box in es6 so you have to do a few extra steps to define it.)
The issue I encountered is that if have symlinked architect-plugin-search in another application for development, the path to the launchSearch.js file is different than if it is not symlinked. When symlinked the path to the file points to my local architect-plugin-search repo which is a different path than when it is in node_modules.
Presumably the two are in sync since they are a simlink, so whatever changes you make in the imported package from the node_modules directory will be reflected in your local repo. Which makes this not really a big deal (it will run either way). It just is weird that the path would point to the linked repo instead of the node_module within the application. So my solution is just to define __dirname and leave it at that. It can run in the symlinked repo or it can run in the node_modules if not symlinked. Either way works.
So I just ended up removing the check to see if node_modules is in the path. It'll work either way, it'll just be pointing to the external repo instead of the node_modules if symlinked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand now that
__filename
only works in CommonJS modules. Just useimport.meta.url
instead. No need to convert it to a path, becausechild_process.fork
accepts URLs.Why is it a problem that this might return the target of the symlink?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import.meta.url is
architect-plugin-search/index.js
so that can't be used directly. Here is what I'm doing there and why it needs to have multiple steps:file:///Users/courey/dev/architect-plugin-search/index.js
this is getting the name of the file that is currently running. in this case it'sindex.js
. I don't wantindex.js
, I wantlaunchSearch.js
.fileURLToPath
so __filename would then be:/Users/courey/dev/architect-plugin-search/index.js
. This is preparing it so that we can use path.fileURLToPath
we can usepath.dirname()
to get just the path of the directory instead of the file that is running. that makes __dirname:/Users/courey/dev/architect-plugin-search
/launchSearch.js
to the end of that path to have the proper path and proper file name.stuff that doesn't work
If I just used
import.meta.url
we get this error:if I added
/launchSearch.js
to theimport.meta.url
we get:import.meta.url passed to fileURLToPath and appended with file gives us this:
/Users/courey/dev/architect-plugin-search/index.js/launchSearch.js
import.meta.url passed to path.dirname():
file:///Users/courey/dev/architect-plugin-search
if you pass
import.meta.url
directly intopath.dirname()
you get this:file:///Users/courey/dev/architect-plugin-search
If I skipped all that and went straight to using
import.meta.dirname
(which should exist in nodejs 20.11) instead it is undefined, which would give us:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it undefined because you are doing a CommonJS build rather than an ESM build?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for posterity in case someone gets here and doesn't read all the other comments, we answered this in another comment. It is not supported prior to node v.20.11.0, so it is undefined in any version earlier than that.