Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accessing file from current directory works, but accessing configuration files do not #439

Closed
gldraphael opened this issue Apr 4, 2019 · 2 comments
Labels

Comments

@gldraphael
Copy link

Stacktrace:

System.TypeInitializationException: The type initializer for 'Submission#0' threw an exception. ---> System.IO.FileNotFoundException: The configuration file 'config.json' was not found and is not optional. The physical path is '/usr/local/lib/dotnet-script/config.json'.
   at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
   at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
   at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
   at Submission#0..cctor() in /Users/gldraphael/projects/misc/vi/config.csx:line 9
   --- End of inner exception stack trace ---
   at Submission#0.<<Initialize>>d__0.MoveNext() in /Users/gldraphael/projects/misc/vi/config.csx:line 14
--- End of stack trace from previous location where exception was thrown ---
   at Dotnet.Script.Core.ScriptRunner.Execute[TReturn](String dllPath, IEnumerable`1 commandLineArgs) in C:\projects\dotnet-script\src\Dotnet.Script.Core\ScriptRunner.cs:line 58

Source:

#!/usr/bin/env dotnet-script
#r "nuget: Microsoft.Extensions.Configuration.Json, 2.1.1"
#r "nuget: Microsoft.Extensions.Configuration.Binder, 2.1.1"
#load "serilog.csx"

using Microsoft.Extensions.Configuration;
using Serilog;

public static var config = new ConfigurationBuilder()
          .AddJsonFile("config.json", optional: false, reloadOnChange: true)
          .AddJsonFile("config.secrets.json", optional: true, reloadOnChange: true)
          .Build()
          .Get<Config>();
Log.Verbose(config.Secret);
public class Config { public string Secret { get; set; } }

File structure:

~
  - config.csx
  - config.json
  - config.secrets.json

However, another script that accesses a file works just fine:

#!/usr/bin/env dotnet-script
#r "nuget: SubtitlesParser, 1.4.7"
#load "serilog.csx"

using Serilog;
using SubtitlesParser.Classes.Parsers;

var parser = new VttParser();
using (var fileStream = File.OpenRead("captions.vtt"))
{
    var items = parser.ParseStream(fileStream, Encoding.UTF8);
    Log.Verbose("{@items}", items);
}
~
  - parse.csx
  - captions.vtt

serilog.csx

#!/usr/bin/env dotnet-script
#r "nuget: Serilog.Sinks.Console, 3.1.1"

using Serilog;

Log.Logger = new LoggerConfiguration()
    .MinimumLevel.Verbose()
    .WriteTo.Console()
    .CreateLogger();

Possibly related: #435

@filipw
Copy link
Member

filipw commented Apr 8, 2019

@seesharper is this caused by us not resetting the current directory correctly? since the script runs from the temp folder

@filipw filipw added the bug label Apr 8, 2019
@seesharper
Copy link
Collaborator

seesharper commented Apr 10, 2019

This is related to how Microsoft.Extensions.Logging resolves the full path to the config file when you give it a relative path. My guess here is that it is resolved based on the entry assembly which is dotnet-script.

There is really not much we can do about this other than providing the full path to the config file.

Like this:

public static var config = new ConfigurationBuilder()
          .AddJsonFile(Path.GetFullPath("config.json"), optional: false, reloadOnChange: true)
          .Build()
          .Get<Config>();

@filipw filipw added question and removed bug labels Jun 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants