Why a function returns two different values if copied into a Notebook or imported with Get from a Wolfram Mathematica Package?

Update

This bug has been fixed in the just released Mathematica 11.0.


This appears to be a difference in parsing between the frontend and the kernel.

Compare (in a notebook)

HoldForm[⌈x⌉ + 1]

(* Ceiling[x] + 1 *)

with

Get[StringToStream["HoldForm[⌈x⌉ + 1]"]]

(* Ceiling[x] (+1) *)

where the latter has multiplication instead of addition.

One possible workaround is to just add parentheses in the .wl file, e.g.

(⌈Log[2, 1 + Abs[bar[x] - 1]]⌉) + 1

It seems to me that setenv is being used here to set the values of a series of helper variables that are then used by the other functions in the code. This (is awful but) works within a single notebook because all those variables are visible to all functions.

I suspect, however, that once you put the code in a package, you run into context problems. Those definitions made by setenv will apply to variables within the package, belonging to a different context. I am not sure where the problem lies exactly that generates the incorrect result (code is too long and I don't have time to chase it around), but I suspect that when you run one of the other packaged functions, they somehow end up dealing with either a wrong initialization value, or with an altogether initialized variable.