Skip to content
This repository has been archived by the owner on Dec 18, 2018. It is now read-only.

AddJsonFile throws invalid directory name when optional is true #478

Closed
dankellett opened this issue Jul 8, 2016 · 5 comments
Closed

AddJsonFile throws invalid directory name when optional is true #478

dankellett opened this issue Jul 8, 2016 · 5 comments
Assignees

Comments

@dankellett
Copy link

dankellett commented Jul 8, 2016

var builder = new Microsoft.Extensions.Configuration.ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile(@"C:/myDirectory/mySub/appsettings.json", optional: true)

When adding a configuration file during app startup, the above code will throw an exception if the "C:/myDirectory/mySub/" directory does not exist. Since this is set as an optional file, the directory should not need to exist.

@HaoK HaoK added the bug label Jul 9, 2016
@HaoK HaoK added this to the 1.0.1 milestone Jul 9, 2016
@HaoK HaoK self-assigned this Jul 9, 2016
@HaoK
Copy link
Member

HaoK commented Jul 11, 2016

Related aspnet/FileSystem#199

@victorhurdugaci
Copy link
Contributor

We really need this to work when the directory is missing because in Antares we watch some config files that don't exist yet, not their parent folder is there. So the configuration (and the file watcher) should support watching a folder and files in folders that don't exist yet.

It's required by the Antares integration work items. cc @glennc

@HaoK
Copy link
Member

HaoK commented Jul 23, 2016

Looks like we can fix this for absolute paths by walking up the path and creating the physical file provider against that directory leaving the rest of the path for the file, i.e.

Updating the code here: https://github.com/aspnet/Configuration/blob/dev/src/Microsoft.Extensions.Configuration.Ini/IniConfigurationExtensions.cs#L76

            if (provider == null && Path.IsPathRooted(path))
            {
                // Find a directory that exists to root the file provider against
                var directory = Path.GetDirectoryName(path);
                var pathToFile = Path.GetFileName(path);
                while (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
                {
                    pathToFile = Path.Combine(Path.GetFileName(directory), pathToFile);
                    directory = Path.GetDirectoryName(directory);
                }
                provider = new PhysicalFileProvider(directory);
                path = pathToFile;
            } 

Sound reasonable?

cc @divega

@victorhurdugaci
Copy link
Contributor

I think that would work.. My only concern is perf if you end up watching the disk's root and there's a lot of file watching.

Btw, that change is not just for ini files, right?

@HaoK
Copy link
Member

HaoK commented Jul 28, 2016

3ea6708

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants