question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

compilation unit 'FSharp.Core' did not contain the namespace, module or type 'StringModule'

See original GitHub issue

Description

I wrote a simple type provider that references String.length in the untyped quotation used to provide a constructor implementation. The type provider library builds, but a consuming project fails to build with the following error:

FSC : error FS0193: The module/namespace 'Microsoft.FSharp.Collections' from compilation unit 'FSharp.Core' did not contain the namespace, module or type 'StringModule' [/Users/ben/proj/oss/constrainedtypes/test/ConstrainedTypes.Test/ConstrainedTypes.Test.fsproj]

The type provider is implemented as follows:

namespace ConstrainedTypes
​
open System.IO
open System.Reflection
open FSharp.Quotations
open ProviderImplementation
open ProviderImplementation.ProvidedTypes
open FSharp.Quotations
open Microsoft.FSharp.Quotations
​
[<TypeProvider>]
type ConstrainedStringProvider (config : TypeProviderConfig) as this =
    inherit TypeProviderForNamespaces (config, addDefaultProbingLocation=true)
​
    let rootNs = "ConstrainedTypes"
    let thisAssembly = Assembly.GetExecutingAssembly()
    let boundedStringProvider = ProvidedTypeDefinition(thisAssembly, rootNs, "BoundedString", Some typeof<string>)
​
    do
        boundedStringProvider.DefineStaticParameters([ProvidedStaticParameter("Length", typeof<int>)], fun name args ->
            let length = args.[0] :?> int
            let provided = ProvidedTypeDefinition(thisAssembly, rootNs, name, Some typeof<string>)
​
            ProvidedConstructor(
                [ProvidedParameter("value", typeof<string>)],
                fun args ->
                    <@@
                        printfn "checking bound: %d" length
                        if (length < String.length %%(args.[0])) then
                            sprintf "provided value exceeded the bounds: '%s' > %d"
                                %%(args.[0]) length
                            |> invalidArg "value"
                    @@>
            )
            |> provided.AddMember
​
            provided
        )
​
        this.AddNamespace(rootNs, [boundedStringProvider])
​
[<TypeProviderAssembly>]
do ()

This implementation can be cloned here: https://github.com/aggieben/constrainedtypes

Repro steps

See implementation above. Clone the linked implementation and build the ConstrainedTypes.Test project to repro.

Expected behavior

I expected the use of the provided type BoundedString<10> to compile.

Actual behavior

The type provider fails at consumer compile time.

Known workarounds

None

Related information

  • MacOS Catalina
  • .NET Core 3.1.100

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sergey-tihoncommented, Jun 26, 2020

ops, sorry, my typo As error message says issue is in method accessability. You need to remove internal modifier from module declaration

Replace

module internal Utilities =

to

module Utilities =
1reaction
sergey-tihoncommented, Jun 25, 2020

Thank you, it definitely looks like a bug of mapping types from quotation to target runtime types.

It is a good practice to keep quotations as small as possible. You can move method implementation into runtime assembly and just call it from quotation.

[<AutoOpen>]
module internal Utilities =

    let x = 1

    let ctorBoundedString length str = 
        if (length < String.length str) then
            sprintf "provided value exceeds the bounds: '%s' > %d"
                str length
            |> invalidArg "value"

simple quotation that call implementation and pass parameters

            ProvidedConstructor(
                [ProvidedParameter("value", typeof<string>)],
                fun args -> <@@ ctorBoundedString length %%(args.[0]) @@>
            )
            |> boundedStringType.AddMember
Read more comments on GitHub >

github_iconTop Results From Across the Web

Build error: The module/namespace 'Microsoft.FSharp. ...
FSharp.Core' from compilation unit 'FSharp.Core' did not contain the namespace, module or type 'string' #2977.
Read more >
F# 3.0 compiler error FS0193 when upgrading project
RuntimeHelpers' from compilation unit 'FSharp.Core' did not contain the namespace, module or type 'LeafExpressionConverter'.
Read more >
Modules - F# | Microsoft Learn
Learn how an F# module is a grouping of F# code, such as values, types, ... The qualified namespace does not have to...
Read more >
FSharp.Core
Indicates a construct is automatically opened when brought into scope through an assembly reference or then opening of the containing namespace or module....
Read more >
FSharpSpec-4.1-latest
the f# 4.1 language specification note: this documentation is the specification ... without the type annotation, the f# compiler would not have known...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found