11
$\begingroup$

Bug introduced in V10.4 or earlier and persisting through V11.3

[CASE:4072996]


SystemOpen @ Export[
  FileNameJoin[{$TemporaryDirectory, "test.m" }]
, "x = \"
    (* ::Chapter:: *)
    (* Metadata*)
  \""
, "Text"
]

enter image description here

Well, I didn't expect this. It wasn't how I faced it, I just had a string template for a package inside a package and when I reopened the file it was corrupted.

Is this expected? Shouldn't expressions have priority over package tokens. The more that it is hard to find any documentation about (* ::Section:: *) and friends.

What practices should I follow to not be hit by this again?

$\endgroup$
  • 2
    $\begingroup$ Wow now that is a nasty bug. WRI's package parse is already pretty slow. Good to know it's somewhat unreliable too! $\endgroup$ – b3m2a1 May 24 '18 at 20:18
  • $\begingroup$ @b3m2a1 I just noticed it is even worse, I assumed it only happens when the line starts with the token, but even whatever code "(* ::Chapter:: *)" will break it :P $\endgroup$ – Kuba May 25 '18 at 7:35
  • $\begingroup$ Would it be acceptable to escape the special tag as \(* or (\*? I think they must get prior treatment before standard parsing. $\endgroup$ – Silvia Jun 2 '18 at 16:02
  • $\begingroup$ @Silvia I will try later but it depends how much hassle it would be to use it and what implications it would have, e.g. I want to Export["test.m", x, "Text"] and it should open as a valid package with chapter correctly parsed as a real token. $\endgroup$ – Kuba Jun 4 '18 at 13:00
1
$\begingroup$

Maybe you can try this, bearing in mind that Mathematica is really a bootstrapping system, i.e., it can fix itself (except Dynamic, that seems pretty unfixable)

file = FileNameJoin[{$TemporaryDirectory, "test.m"}];
Export[file,
  "x = \"
      (* ::Chapter:: *)
      (* Metadata*)
    \"", "Text"];
bin = Import[file, "Text"];
Export[file, "", "Text"];
NotebookWrite[nb = NotebookOpen[file], ExportString[bin, "Text"]];
FrontEndExecute[FrontEndToken[nb, "Save"]]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ 1) next time you open the file the issue persists so it would have to be a default action to open .m files 2) which can't be enforced if you just double click on a .m file from e.g. desktop. $\endgroup$ – Kuba May 24 '18 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.