-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Allow user-added globals on the REPL #1661
Conversation
Sorry, just tested—this doesn't actually fix the stated issue. Back to the drawing board... |
@@ -33,12 +33,15 @@ backlog = '' | |||
|
|||
# The REPL context; must be visible outside `run` to allow for tab completion | |||
sandbox = Script.createContext() | |||
excludedGlobals = [ | |||
'global', 'GLOBAL', 'root' | |||
] | |||
nonContextGlobals = [ |
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.
Isn't this unused then?
OK, patch modified so globals are reloaded every time you run a command on the REPL. This fixes #1654, but at the cost of making it impossible to override globals defined elsewhere within the REPL. That is, if you
and then type
on the REPL, @satyr Yes, that was a (more minor) mistake. Fixed. |
I'm starting to think it'd be better to just use the "real" |
In fact, there's no way to get the desired behavior while
then there's no way for the REPL to determine that
but should be 23 after the line
The REPL can see that both Now, come to think of it, we're already injecting the sandbox's Sorry for all the thinking-out-loud today... |
... even after all this, I still have no idea why someone would want variables defined globaly in another module to be merged with their global scope when they |
Sure, in principle, you want to keep things isolated. But in practice, attaching things directly to So check this out: I came up with what seemed like a workable hack, since we're overriding the
...which looks crazy, but it works—kinda. It turns out that |
So. I spent some time finding a way around it, but I think the best (possibly only) solution is to ditch Also, the |
I'm still not convinced at all that there's a reason you want a module having access to your scope. It's a "time saver"? How? Even if that was true, it violates my principle of least privilege. In the current model, I trust modules only with objects to which I give them references. I don't want to As for the node requirement, I don't think it's unreasonable at all to bump it to |
Agreed about the package.json commit; moved to #1663. |
@michaelficarra You make a good point about limiting a module's access to the requiring process's global scope. But note that such a limitation doesn't exist when programs are run and modules are loaded with Even in node's REPL there is some "weirdness" with respect to the modules and globals. See my comment on #1654. Supposing I agreed that I did not want a module to access the requiring process's global scope directly .. okay, but the Two thoughts: (1) That require-modules-global behavior varies between running programs in the usual way vs. banging around in the REPL is confusing, at least it seems so to me. (2) While writing modules so as to directly modify their requiring process's global scope may well be an anti-pattern, I would at least like to be able to explicitly "globalize" some properties of my modules. Now I don't do that all the time, but I may write a module the very purpose of which is to (optionally) introduce some "sugar" into the global scope. Suppose I write a module
Now I can make calls to This is entirely possible when running node.js scripts in the regular way, written in both JavaScript and CoffeeScript. But |
I don't think that should be possible. The
in the REPL. Or if you'd like it to be explicit, export a
|
Michael, I still don't understand your opposition.
As you well know, CoffeeScript's
Do you really see a security issue here? I'd be much more worried about things I |
Btw, I think the current patch is pretty good, and I hope you'll take a look. It's actually provides better behavior than the Node REPL's, IMHO—the test I mentioned before, with |
I don't think I have the requisite REPL experience to make a judgement call about this one (I rarely touch the thing). @michaelficarra -- if you're comfortable with the final state of this patch, do you mind reviewing/merging it? In general, I think we should strive for the behavior of the Node.js REPL for issues like this one. |
I just want to note, in a brief way, what I think is the best argument in favor of Trevor's patch: It allows for strong symmetry between how one may manipulate the global environment when running scripts in the normal way and when one is experimenting, developing, learning, debugging in the REPL. Without Trevor's patch, there is an asymmetry between the two ways of running scripts, with respect to manipulating global scope. If Trevor's patch has no implications beyond repairing the asymmetry, then why not accept the patch? If there are implications, what are they? I've been using Trevor's fork, with the globals-REPL patch, and have not yet experienced any drawbacks; on the contrary, I find his patch beneficial. That's my two cents. |
+1 for consistency. I have often found it annoying, when browsers (firefox and chrome with canvas/getImageData) or operating systems (windows) restrict what I can do from a local file - that I just wrote - when no such restriction is imposed on files coming from a web server - and as with this issue if there was to be a difference then it should be the other way round - allowing me to do quick-n-dirty things in the REPL that I can not do in an npm module. When 1.1.2 came out I ran all my code/tests as programs and they all passed. It was surprising to me when it was reported that the code did not work with -r and the REPL. It is a burden having to repeat all the tests manually in the REPL. Remember that it is terribly off-putting when you grab a tutorial and a programming environment, follow the instructions and then it does not work. To minimize the chance of that happening I didn't refer to npm but instead included copies of underscore, coffeekup, qc, etc. I assumed that I would notice changes in CoffeeScript as they happen. I have performed some tests without any issues such as the bash/grep like session I showed in issue #1654. But honestly not a whole lot. I usually work in 2D (in my editor/execution environment) rather than the 1D line oriented REPL. Concerning the bigger question about globals. I can understand if such a restriction is put on modules that come from npm. I think that would be something that requires the cooperation of the node project. BTW Node has a good way of deprecating things, printing: "Use process.env instead of process.ENV". If global is to be retired then issuing a warning in the preceding version is a very user-friendly way to do it. |
I do not agree with the changes to -1 |
Changelog node 0.5.10 (https://github.com/joyent/node/wiki/ChangeLog)
|
Thanks for that, autotelicum. As jashkenas has said,
On that basis alone, this change would seem to be justified. But I'd still like to understand what you're proposing, Michael. You maintain that it's preferable for |
@TrevorBurnham: I just don't like how my |
I just recently experienced some interesting behavior with respect to Trevor's patch and I'm going to experiment with the REPL in node v0.4.12 vs v0.5.10, and compare to coffee's normal REPL vs. Trevor's patch, and will report back shortly. It may be that the coffee REPL should follow more closely the direction taken in node's REPL v0.5.10 rather than adoption of Trevor's patch. |
@michaelsbradleyjr OK, I'll hold off while you look into that. Thanks. |
Summary conclusion: Nevermind, Trevor's patched REPL and the REPL for node v0.5.10 are producing identical results. But this is what I was testing... Consider the following two scripts: b.coffee
a.coffee
In the same directory I ran Here's the output from my REPL sessions... node REPL v0.4.12
node REPL v0.5.10
coffee REPL v1.1.2 (node v0.4.12)
coffee REPL - Trevor's patch (node v0.4.12)
Running |
Cool. Thanks for that, @michaelsbradleyjr. We should definitely get on top of Node 0.5.x compatibility soon. |
Allow user-added globals on the REPL (fixes #1654)
I apologize for commenting on this old, closed issue, but has this been reverted(?) as of 1.5.0? I noticed in 1.5.0 that the CoffeeScript repl now handles global properties differently than the node reply (0.8.19 in my case). Before I opened a new issue, I had hoped @TrevorBurnham could chime in very quickly to let me know if there was something key I was missing about the intended current state of global in the CoffeeScript repl. |
See #1654. The change here is very simple: Rather than specifically listing the globals we want to preserve on the REPL, we list the ones we want to exclude (which are just references to the "real"
global
/root
). This has no effect on the REPL environment whatsoever except when a userrequire
s something that modifiesglobal
, either with a command or with the-r
flag.