Frage

Here is the scenario:

  1. Open Visual Studio. This was done in VS2010 Pro.
  2. Open F# Interactive within Visual Studio
  3. Open project with fsx file
    Note: Project and fsx file are in E:\<directories>\fsharp-tapl\arith
  4. Send commands to F# Interactive from fsx file

    > System.Environment.CurrentDirectory;; 
    val it : string = "C:\Users\Eric\AppData\Local\Temp"
    

    I was not expecting a Temp directory but it makes sense.

    > #r @"arith.exe"
    Examples.fsx(7,1): error FS0082: Could not resolve this reference. 
    Could not locate the assembly "arith.exe". 
    Check to make sure the assembly exists on disk. 
    If this reference is required by your code, you may get compilation errors. 
    (Code=MSB3245)
    
    Examples.fsx(7,1): error FS0084: Assembly reference 'arith.exe' was not found 
    or is invalid
    

    The #r command error shows that F# Interactive currently does not know the location of arith.exe.

    > #I @"bin\Debug"
    --> Added 'E:\<directories>\fsharp-tapl\arith\bin\Debug' 
    to library include path
    

    So we tell F# Interactive the location of the arith.exe. Notice that the path is NOT an absolute path but a sub-path of the project. I have not told F# Interactive the location of the arith project E:\<directories>\fsharp-tapl\arith

    > #r @"arith.exe"
    --> Referenced 'E:\<directories>\fsharp-tapl\arith\bin\Debug\arith.exe'
    

    And F# Interactive correctly finds arith.exe reporting the correct absolute path.

    > open Main
    > eval "true;" ;;
    true
    val it : unit = ()
    

    This confirms that arith.exe was correctly found, loaded and works.

So how did F# Interactive #I command know the project path since the current directory is of no help?

What I am really after is from within F# Interactive how does one get the path to the project, E:\<directories>\fsharp-tapl\arith.

EDIT

> printfn __SOURCE_DIRECTORY__;;
E:\<directories>\fsharp-tapl\arith
val it : unit = ()
War es hilfreich?

Lösung

In F# Interactive, the default directory to search is the source directory. You can query it easily using __SOURCE_DIRECTORY__.

This behaviour is very convenient to allow you to use relative paths. You often have fsx files in the same folder with fs files.

#load "Ast.fs"
#load "Core.fs"

When your refer to a relative path, F# Interactive will always use the implicit source directory as the starting point.

#I ".."
#r ... // Reference some dll in parent folder of source directory
#I ".."
#r ... // Reference some dll in that folder again

If you want to remember the old directory for next reference, you should use #cd instead:

#cd "bin"
#r ... // Reference some dll in bin
#cd "Debug"
#r ... // Reference some dll in bin/Debug
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top