I am using the XML Type Provider with FSharp.Data. I have a line like this:
type internal SomethingFromXML = XmlProvider<"./Sample.xml", EmbeddedResource="MyLib, Sample.xml">
I set the Sample.xml to "Embedded Resource, Do Not Copy".
I packed the MyLib to a NuGet package and used it in another program. But when I now execute it, I get a DirectoryNotFoundException saying it can't find C:\Path\To\My\SolutionWithMyLib\MyLib\Sample.xml
So, it seems to ignore the EmbeddedResource directive. Is that not supported with the XML Provider, or am I missing anything?
Related
The Bazel Starlark API does strange things with files in external repositories. I have the following Starlark snippet:
print(ctx.genfiles_dir)
print(ctx.genfiles_dir.path)
print(output_filename)
ret = ctx.new_file(ctx.genfiles_dir, output_filename)
print(ret.path)
It is creating the following output:
DEBUG: build_defs.bzl:292:5: <derived root>
DEBUG: build_defs.bzl:293:5: bazel-out/k8-fastbuild/genfiles
DEBUG: build_defs.bzl:294:5: google/protobuf/descriptor.upb.c
DEBUG: build_defs.bzl:296:5: bazel-out/k8-fastbuild/genfiles/external/com_google_protobuf/google/protobuf/descriptor.upb.c
That extra external/com_google_protobuf comes seemingly out of nowhere, and it makes my rule fail:
I tell protoc to generate into ctx.genfiles_dir.path (which is bazel-out/k8-fastbuild/genfiles).
So protoc generates bazel-out/k8-fastbuild/genfiles/google/protobuf/descriptor.upb.c
Bazel fails because I didn't generate bazel-out/k8-fastbuild/genfiles/external/com_google_protobuf/google/protobuf/descriptor.upb.c
Likewise, when I try to call file.short_path on a source file from an external repository, I get a result like ../com_google_protobuf/google/protobuf/descriptor.proto. This seems quite unhelpful, so I just wrote some manual code to strip off the leading ../com_google_protobuf/.
Am I missing something? How can I write this rule in a way that doesn't feel like I'm fighting Bazel the whole time?
Am I missing something?
The basic problem, as you already realized, is that you have two path "namespaces" the one that protoc sees (i.e. import paths) and the one that bazel sees (i.e. the path you pass to declare_file().
2 things to note:
1) All paths declared with declare_file() get the path <bin dir>/<package path incl. workspace>/<path you passed to declare_file()>
2) All actions are executed from <bin dir> (unless output_to_genfils=True in which case this switches to <gen dir> as in your example.
Trying to solve the exact same problem you encountered, I resorted to stripping the known path from the output_file's path to determine which directory to pass as p:
# This code is run from the context of the external protobuf dependency
proto_path = "google/a/b.proto"
output_file = ctx.actions.declare_file(proto_path)
# output_file.path would be `<gen_dir>/external/protobuf/google/a/b.proto`
# Strip the known proto_path from output_file.path
protoc_prefix = output_file.path[:-len(proto_path)]
print(protoc_prefix) # Prints: <gen_dir>/external/protobuf
command = "{protoc} {proto_paths} {cpp_out} {plugin} {plugin_options} {proto_file}".format(
...
cpp_out = "--cpp_out=" + protoc_prefix,
...
)
Alternatives
You may also be able to construct the same path with ctx.bin_dir, ctx.label.workspace_name, ctx.label.package, and ctx.label.name.
Misc.
proto_library recently gained an attribute strip_import_prefix. When used, the above is not correct, as all dependent files are symlinked into a new directory from which they have the relative paths declared with strip_import_prefix.
The path format is:
<bin dir>/<repo>/<package>/_virtual_base/<label name>/<path `import`ed in .proto files>
i.e.
<bin dir>/external/protobuf/_virtual_base/b_proto/google/a/b.proto
Assuming you are building an external repo called protobuf, which contains a BUILD file at its root with a target named b_proto, which in turn, relies on a proto_library wrapping google/a/b.proto AND uses the strip_import_prefix attribute.
I'd like to the use app.config file of my F# to store versioning information. I discovered the FSharp.Configuration type provider which seemed like it'd be simple enough. However, I'm running in to an error I can't diagnose.
Below is a screen shot of a version.config file (identical to the one in the link above) and a scratch pad.
As you can see, calling Settings auto populates a drop-down of everything in the <appSettings> chunk of the config but when I try to run something,
I get an error saying that the thing I'm looking for can't be found in the <appSettings> section of the config file.
What's causing this error, especially considering that it clearly is finding it in the config file, given it's auto-populating? What can I do to prevent this from happening again?
You have bumped into this issue.
When you run the Configuration provider in FSI it will look not for the app's config file but FSI's config file. One way to get around this is by specifying the exe's config file explicitly. Here's an example:
open FSharp.Configuration
open System
type Settings = AppSettings<"app.config">
[<EntryPoint>]
let main argv =
let path = System.IO.Path.Combine [|__SOURCE_DIRECTORY__ ;"bin";"release";"ConfigApplication.exe" |]
Settings.SelectExecutableFile path
Settings.TestBool <- false // change a setting
printfn "%A" Settings.Test2 // read another setting
Console.ReadLine() |> ignore
0 // return an integer exit code
This will take the App.config file in the source directory, but use the ConfigApplication.exe.config file in the binaries directory.
If you just need to set the DB's connection string, it's actually easier, if the SQL type provider has a config setting parameter, just specify the config file there (and set it to Always copy in VS), if you add that to .gitignore you can have many different app.config files with different connection strings.
You could also use the YAML provider, it has two advantages, it's not XML and it's not an erasing type provider.
I was able to compile and run an .exe built in XP Mode with Visual Studio 2003. I then noticed the version of the source I was using was not up to date (based on a version number string). So I did a "Get Latest Version (Recursion)"
That gave me the correct version number, but also over 100 Build Errors, of thes types:
Two of these, but 2-clicking them goes nowhere:
"Error reading resources from the resource file for the default culture: Invalid ResX input. Could not find valid "resheader" tags for the .NET Compact Framework ResX reader & writer type names. Make sure this Resx file is a .NET Compact Framework Resx file."
Dozens of these; 2-clicking them also takes me nowhere:
"Object type cannot be converted to target type."
10 or a dozen of these:
"Resource transformation for file 'frmAbout.resx' failed. Invalid ResX input. Could not find valid "resheader" tags for the .NET Compact Framework ResX reader & writer type names. Make sure this Resx file is a .NET Compact Framework Resx file."
2-clicking those gave me this nice "Data for Data" grid:
Finally, several like this:
"'PDAClient.PrinterPickerForm' does not contain a definition for 'AutoScaleMode'"
...which take me to code like this:
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Inherit;
Why would simply getting the latest version of code cause all this mayhem?
I am using the type expression:
type dbSchema = SqlDataConnection<ConnectionStringName="X1", ConfigFile="App.config">
This works great at compile time (I have full access to all the db types), but it fails at run time. I presume it's because the config file generated in the console application's bin directory is named something else, such as MyAppName.exe.config, and therefore the App.config file is not found.
Certainly, for an ASP.NET MVC type app that uses web.config, there's no issue because the compile and runtime config filenames are the same.
Fortunately, placing a duplicate App.config in the bin directory does remediate the problem, but is that what we are expected to do? Any thoughts?
The description of how the type provider definition works is misleading - the value in the typedef really only matters at code/compile time, and as a default at runtime. However, as you've noted, it isn't very smart about finding the correct config file at runtime.
You can accomplish what you want by passing the connection string as a parameter to GetDataContext:
type dbSchema = SqlDataConnection<ConnectionStringName="X2">
let db = dbSchema.GetDataContext(ConfigurationManager.ConnectionStrings.["X2"].ConnectionString)
...or if you also want to make it work in F# interactive, wrap it like so:
type dbSchema = SqlDataConnection<ConnectionStringName="X2">
#if COMPILED
let db = dbSchema.GetDataContext(ConfigurationManager.ConnectionStrings.["X2"].ConnectionString)
#else
let db = dbSchema.GetDataContext()
#endif
(Note that you will need a reference to System.Configuration.)
I don't have a VS2012 on this PC but this should be what you're looking for :
let exeConfigFile = Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().Location) + ".config"
let defaultConfigFile = "App.config"
let configFile = if File.Exists(exeConfigFile) then exeConfigFile else defaultConfigFile
type dbSchema = SqlDataConnection<ConnectionStringName="X1", ConfigFile=configFile>
I have a Spring.NET program with a configuration file. To smooth the transition from test to prod I'd like to have an environment variable that gives the path to a shared config file, and use that to import a resource, however it appears the <import resource="path"/> is not being resolved. For example if I try to load the file <import resource="\\server\share\${computername}\SpringConfig.xml"/> I get a file not found exception as below:
System.Configuration.ConfigurationErrorsException: Error creating context 'spring.root': file [\server\share\${computername}\SpringConfig.xml] cannot be resolved to local file path - resource does not use 'file:' protocol. ---> Spring.Objects.Factory.ObjectDefinitionStoreException: IOException parsing XML document from file [\server\share\${computername}\SpringConfig.xml] ---> System.IO.FileNotFoundException: file [\server\share\${computername}\SpringConfig.xml] cannot be resolved to local file path - resource does not use 'file:' protocol.
Is there a way I can have a dynamic import path in Spring.NET, preferably without writing code?
You can do that anyway with some extra code:
Create your own FileSystemResource that will replace placeholders in the resource name. Start from overriding the existing FileSystemResource (In Spring.Core.IO namespace)
Register your new IResource implementation in the container using your own protocol name (ex: myfile://) See ref docs here for an example :
In .NET configuration file (app.config/web.config)
http://www.springframework.net/doc-latest/reference/html/resources.html#d4e2911
In Spring configuration files
http://www.springframework.net/doc-latest/reference/html/objects.html#context-custom-resourcehandler
Use it!
resource="myfile://\server\share\${computername}\SpringConfig.xml"
I don't think we can do that with the current version.
Latest Java version supports it, so we can expect this feature in a future version (Using variables environnement by default)