Canonical way to check if a project is a package?

I’m trying to write some code to detect if a Project.toml defines a package or not (making a better error output in this issue Requesting better error message for non-packages · Issue #60 · GunnarFarneback/LocalRegistry.jl · GitHub). Right now, I’m just checking whether it has a UUID (after loading it via Pkg.Types.read_project). I can upgrade that check by making sure there’s a file called $(project_dir)/src/$(package_name)[.jl], but really, to be most correct, I need to make sure that file defines a module with the same name, and I’m not sure that I know how to do that. So:

a) Is there already a canonical way to check this?
b) How can I check if that module definition occurs?
c) Is that check sufficient?

This is quite possibly too naive a thought but:

Could you try activating the Project.toml for that given project or package (let’s say the name is YourPackage) and then run the following snippet:

    using YourPackage
    using Pkg

    pkg_name = "YourPackage"
    m = Pkg.Operations.Context().env.manifest

    v_key = findfirst(v-> == pkg_name, m)
    if isnothing(v_key)
        error("Version not found! Is this a valid Julia package?")
        v = m[v_key].version
        println("Package version is $v")
catch e
    println("$e: Is this a valid Julia package?")

This solution was inspired by a stack overflow post here: julia - Determine version of a specific package - Stack Overflow

It does assume a manifest exists for the environment however so that may derail this solution potentially. To my understanding, this would at least solve b), for a) maybe see this discourse post for more: How to test if package is installed - #4 by Tamas_Papp and this one: Test for existence of a variable - #39 by Tamas_Papp for c) I would say that the checks may have to be up to more how you structure or want to administrate your local registry… I would say that could be sufficient personally, but I haven’t run a registry myself yet. :stuck_out_tongue:

Thanks for the suggestion (and the links). I think you’re right that that will work, but I’d rather avoid using YourPackage at any point. That can be an annoyingly long step for some packages.

Could you scan Project.toml for a uuid?

We ended up doing three things: 1) check a UUID exists, 2) check a version exists, 3) check Project/src/Project.jl exists (with some allowances for the extension)

1 Like