Debugging scripts

164 views
Skip to first unread message

domehead100

unread,
Apr 13, 2012, 2:20:13 AM4/13/12
to CS-Script
I was experimenting with a temporary, quick-and-dirty method for
debugging a script this evening and hit an interesting issue.

My attempt was to put a System.Diagnostics.Debugger.Break() statement
in the script. My assumption was that the JIT debugger (Visual Studio
2010 at this point on my machine) would pop up and allow me to debug
the script assembly.

Well, this almost worked, except the compile process in CS-Script is
apparently to write the script code to a temporary file in the temp
directory, compile it, and then remove the temp file (maybe even a
temp assembly is created, not sure).

The problem in the debugger was that the debug symbols reference that
temporary filename when launching the debugger, and of course the file
can't be found because it was removed after the compilation.

In my case, I don't have a script file on the filesystem anyway since
the scripts come from a database; so I guess I'll have to write one
out from a database and then manually point Visual Studio at it; but
it should work. It would be nice if there was some way that the
temporary script filename could be obtained during compilation, so I
could tuck that away and create the file if needed, and not have to
manually point Visual Studio at a script file.

Just wondering if anyone has any thoughts on this. I don't see any
particularly great solution since csc.exe requires files to compile
(until Roslyn I guess) and since I'm not using actual script files
anyway.

I've peeked at using Visual Studio Shell in integrated mode, might try
that. Also have looked into hosting SharpDevelop inside my app (this
would be my preferred solution, to allow full-blown intellisense in
the app while editing) or worst case having it installed with the app
on non-developer workstations. Don't have time to get either of those
first two items working at this point, so I was looking for something
quick and dirty for now. I also can't figure out how to make
SharpDevelop the JIT debugger. I guess I could also use WinDbg.
Also, ActiPro's syntax editor is quite good in terms of editing, but
I'd have to write code around it to enable some kind of debugging
support.

Oleg Shilo

unread,
Apr 13, 2012, 9:06:13 AM4/13/12
to cs-s...@googlegroups.com
>It would be nice if there was some way that the temporary script filename could be obtained during compilation, so I could tuck that away and create the file if needed, and not have to
manually point Visual Studio at a script file. 

This is how the temp file is created and as you can see it is not possible to predict the file name and it's location:
         string tempFile = CSExecutor.GetScriptTempFile();
         try
         {
             using (StreamWriter sw = new StreamWriter(tempFile))
             {
                 sw.Write(scriptText);
             }
             return Compile(tempFile, assemblyFile, debugBuild, refAssemblies);

           ...

        static public string GetScriptTempFile()
        {
            lock (typeof(CSExecutor))
            {
                return Path.Combine(GetScriptTempDir(), Guid.NewGuid().ToString() + ".tmp");
            }
        }
However you do not have to use CompileCode(). You can save the code to any location of your choice and then call Compile(). So you have full control over the source code file naming and the location.

> I don't see any particularly great solution since csc.exe requires files to compile (until Roslyn I guess) and since I'm not using actual script files anyway.
You are absolutely correct. Can't wait till Roslyn is fully out so I can replace System.CodeDom.Compiler.ICodeCompiler
Still do not understand why it was not available from the beginning of .NET. 

Oleg
Reply all
Reply to author
Forward
0 new messages