From c1b4816208588a30cee7e8e2d42c7732d77db4aa Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Sat, 7 Jul 2018 10:42:12 +0200 Subject: [PATCH] make reading packages a bit more robust (#476) --- stdlib/Pkg/src/Operations.jl | 4 ++-- stdlib/Pkg/src/Types.jl | 21 +++++++++++++++++---- stdlib/Pkg/test/repl.jl | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/stdlib/Pkg/src/Operations.jl b/stdlib/Pkg/src/Operations.jl index d40e45096b6397..dbf880d5fdc4c4 100644 --- a/stdlib/Pkg/src/Operations.jl +++ b/stdlib/Pkg/src/Operations.jl @@ -133,7 +133,7 @@ function collect_project!(ctx::Context, pkg::PackageSpec, path::String, fix_deps project_file = projectfile_path(path) fix_deps_map[pkg.uuid] = valtype(fix_deps_map)() (project_file === nothing) && return false - project = read_project(project_file) + project = read_package(project_file) compat = get(project, "compat", Dict()) if haskey(compat, "julia") if !(VERSION in Types.semver_spec(compat["julia"])) @@ -839,7 +839,7 @@ function collect_target_deps!(ctx::Context, pkgs::Vector{PackageSpec}, pkg::Pack end project = nothing if project_path !== nothing - project = read_project(project_path) + project = read_package(project_path) end # Pkg2 compatibiity with test/REQUIRE diff --git a/stdlib/Pkg/src/Types.jl b/stdlib/Pkg/src/Types.jl index cddfeb6ac58d2d..acc1c6d90fd393 100644 --- a/stdlib/Pkg/src/Types.jl +++ b/stdlib/Pkg/src/Types.jl @@ -22,7 +22,7 @@ export UUID, pkgID, SHA1, VersionRange, VersionSpec, empty_versionspec, CommandError, cmderror, has_name, has_uuid, write_env, parse_toml, find_registered!, project_resolve!, project_deps_resolve!, manifest_resolve!, registry_resolve!, stdlib_resolve!, handle_repos_develop!, handle_repos_add!, ensure_resolved, manifest_info, registered_uuids, registered_paths, registered_uuid, registered_name, - read_project, read_manifest, pathrepr, registries, + read_project, read_package, read_manifest, pathrepr, registries, PackageMode, PKGMODE_MANIFEST, PKGMODE_PROJECT, PKGMODE_COMBINED, UpgradeLevel, UPLEVEL_FIXED, UPLEVEL_PATCH, UPLEVEL_MINOR, UPLEVEL_MAJOR, PackageSpecialAction, PKGSPEC_NOTHING, PKGSPEC_PINNED, PKGSPEC_FREED, PKGSPEC_DEVELOPED, PKGSPEC_TESTED, PKGSPEC_REPO_ADDED, @@ -371,6 +371,19 @@ function read_project(file::String) isfile(file) ? open(read_project, file) : read_project(devnull) end +_throw_package_err(x, f) = cmderror("expected a `$x` entry in project file at $(abspath(f))") +function read_package(f::String) + project = read_project(f) + haskey(project, "name") || _throw_package_err("name", f) + haskey(project, "uuid") || _throw_package_err("uuid", f) + name = project["name"] + entry = joinpath(dirname(f), "src", "$name.jl") + if !isfile(entry) + cmderror("expected the file `src/$name.jl` to exist for package $name at $(dirname(f))") + end + return project +end + function read_manifest(io::IO) manifest = TOML.parse(io) for (name, infos) in manifest, info in infos @@ -590,17 +603,17 @@ function parse_package!(ctx, pkg, project_path) env = ctx.env project_file = projectfile_path(project_path) if project_file !== nothing - project_data = parse_toml(project_file) + project_data = read_package(project_file) pkg.uuid = UUID(project_data["uuid"]) pkg.name = project_data["name"] if haskey(project_data, "version") pkg.version = VersionNumber(project_data["version"]) else - @warn "project file for $(pkg.name) is missing a `version` entry" + @warn "project file for $(pkg.name) at $(project_path) is missing a `version` entry" Pkg.Operations.set_maximum_version_registry!(env, pkg) end else - @warn "packages will need to have a [Julia]Project.toml file in the future" + @warn "package $(pkg.name) at $(project_path) will need to have a [Julia]Project.toml file in the future" if !isempty(ctx.old_pkg2_clone_name) # remove when legacy CI script support is removed pkg.name = ctx.old_pkg2_clone_name else diff --git a/stdlib/Pkg/test/repl.jl b/stdlib/Pkg/test/repl.jl index 441d2a1bedcef0..7f50b1f14839e5 100644 --- a/stdlib/Pkg/test/repl.jl +++ b/stdlib/Pkg/test/repl.jl @@ -54,6 +54,24 @@ temp_pkg_dir() do project_path LibGit2.with(LibGit2.GitRepo(devdir)) do repo @test LibGit2.branch(repo) == "DO_NOT_REMOVE" end + + withenv("USER" => "Test User") do + pkg"generate Foo" + end + pkg"dev Foo" + mv(joinpath("Foo", "src", "Foo.jl"), joinpath("Foo", "src", "Foo2.jl")) + @test_throws CommandError pkg"dev Foo" + mv(joinpath("Foo", "src", "Foo2.jl"), joinpath("Foo", "src", "Foo.jl")) + write(joinpath("Foo", "Project.toml"), """ + name = "Foo" + """ + ) + @test_throws CommandError pkg"dev Foo" + write(joinpath("Foo", "Project.toml"), """ + uuid = "b7b78b08-812d-11e8-33cd-11188e330cbe" + """ + ) + @test_throws CommandError pkg"dev Foo" end end