# Importing Packages to .wls without shadow definitions and context specification

I am trying to import a package that works correctly when I use it in a notebook in a wolframscript .wls. I get a weird shadowing problem, however.

Here is a minimal working example test.wls, using the example from the Needs documentation:

#!/usr/bin/env wolframscript -print All

Needs["ComputerArithmetic"]

Ulp[1000.]


after chmod +x test.wls the result of running it is

> ./test.wls

Ulp::shdw: Symbol Ulp appears in multiple contexts {ComputerArithmetic, Global};
definitions.
GlobalUlp[1000.]


However, when I copy and paste the two non-shebang lines of code from test.wls into a notebook, I get the correct result. The same thing happens with my custom package.

I tried runnning wolframscript -code '$Path' and the directory that contains my package is there, so it cannot be a screwed up path. If I change the final line of my script to ComputerArithmeticUlp[1000.]  I get as output 1.1368683772161603*^-13, and the same fix works for my own package. Question: what is going on here, and what is a good workaround so that I don't have to specify the context of every custom package procedure and variable? • For completeness, I have $Version=="12.0.0 for Mac OS X x86 (64-bit) (April 7, 2019)" – evanb Nov 27 '19 at 1:14
• I guess I should also say that I have wolframscript --version WolframScript 1.2.0 for MacOSX-x86-64. – evanb Nov 27 '19 at 4:45
• It seems to be fine if you remove -print All and use e.g. Echo inside. – Kuba Nov 28 '19 at 8:48
• You're right, that is a solution. But the documentation reference.wolfram.com/language/ref/program/wolframscript.html suggests I should be able to use -print All (or maybe all) to get everything printed. In my "actual" script, rather than my mwe, making this change would be very very annoying. – evanb Nov 29 '19 at 15:11

Here's a version of the script that works:

#!/usr/bin/env wolframscript -print All -run \'Needs[\"ComputerArithmetic\"]\'

Ulp[1000.]


This is a limitation of -print All that we probably should either document or find a fix for, though the latter is rather challenging. When executing a script normally or using -print, wolframscript can essentially tell the kernel to just Get the script file and print or not the value returned by Get. But for -print All, wolframscript effectively has to import the script prior to executing it to get all the individual expressions. This means the all the symbols are created prior to the Needs evaluating, and the package symbols end up in the wrong context. Using the -run` option to the kernel (without lots of shell escapes) tells the kernel to load the package prioir to doing anything, so the symbols are created in the right context.

Update

After thinking about this some more, it probably doesn't actually have to import the file first--it's probably just doing that for efficiency. It could read it in one expression at a time, evaluate the expression, and print the result. This would be slower, but produce more accurate results. I'll file a bug about this.

• Why does it have to import the script prior to execution, rather than reading it line-by-line (or similar) and executing it on the fly? – evanb Dec 2 '19 at 20:23