Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect Project.toml in parent dirs and disable PlutoPkg #1281

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/evaluation/WorkspaceManager.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module WorkspaceManager
import UUIDs: UUID
import ..Pluto: Configuration, Notebook, Cell, ProcessStatus, ServerSession, ExpressionExplorer, pluto_filename, Token, withtoken, Promise, tamepath, project_relative_path, putnotebookupdates!, UpdateMessage
import ..Pluto.PkgCompat
import ..Pluto.PkgCompat: PkgCompat, AbstractPackageManagement, FullyManaged, ParentProject, NotManaged
import ..Configuration: CompilerOptions, _merge_notebook_compiler_options, _resolve_notebook_project_path, _convert_to_flags
import ..Pluto.ExpressionExplorer: FunctionName
import ..PlutoRunner
Expand Down Expand Up @@ -79,7 +79,7 @@ function make_workspace((session, notebook)::SN; force_offline::Bool=false)::Wor
end

function use_nbpkg_environment((session, notebook)::SN, workspace=nothing)
enabled = notebook.nbpkg_ctx !== nothing
enabled = (notebook.nbpkg_ctx isa FullyManaged)
if workspace.nbpkg_was_active == enabled
return
end
Expand Down
13 changes: 7 additions & 6 deletions src/notebook/Notebook.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import UUIDs: UUID, uuid1
import .ExpressionExplorer: SymbolsState, FunctionNameSignaturePair, FunctionName
import .Configuration
import .PkgCompat: PkgCompat, PkgContext
import Pkg
import .PkgCompat: PkgCompat, PkgContext, AbstractPackageManagement, FullyManaged, ParentProject, NotManaged

mutable struct BondValue
value::Any
Expand Down Expand Up @@ -42,7 +41,7 @@ Base.@kwdef mutable struct Notebook
# nothing means to use global session compiler options
compiler_options::Union{Nothing,Configuration.CompilerOptions}=nothing
# nbpkg_ctx::Union{Nothing,PkgContext}=nothing
nbpkg_ctx::Union{Nothing,PkgContext}=PkgCompat.create_empty_ctx()
nbpkg_ctx::AbstractPackageManagement=FullyManaged()
nbpkg_ctx_instantiated::Bool=false
nbpkg_restart_recommended_msg::Union{Nothing,String}=nothing
nbpkg_restart_required_msg::Union{Nothing,String}=nothing
Expand Down Expand Up @@ -126,7 +125,7 @@ function save_notebook(io, notebook::Notebook)
end


using_plutopkg = notebook.nbpkg_ctx !== nothing
using_plutopkg = notebook.nbpkg_ctx isa FullyManaged

write_package = if using_plutopkg
ptoml_path = joinpath(PkgCompat.env_dir(notebook.nbpkg_ctx), "Project.toml")
Expand Down Expand Up @@ -267,7 +266,7 @@ function load_notebook_nobackup(io, path)::Notebook
end
else
PkgCompat.create_empty_ctx()
end
end |> FullyManaged

appeared_order = setdiff(cell_order ∩ keys(collected_cells), [_ptoml_cell_id, _mtoml_cell_id])
appeared_cells_dict = filter(collected_cells) do (k, v)
Expand Down Expand Up @@ -350,7 +349,9 @@ function move_notebook!(notebook::Notebook, newpath::String; disable_writing_not
if isdir("$oldpath_tame.assets")
mv("$oldpath_tame.assets", "$newpath_tame.assets")
end
notebook
return (
dir_changed=(dirname(oldpath_tame) == dirnmae(newpath_tame)),
)
end

function sample_notebook(name::String)
Expand Down
7 changes: 6 additions & 1 deletion src/notebook/PathHelpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ function cutename()
titlecase(rand(adjectives)) * " " * rand(nouns)
end

const default_notebook_dir = joinpath(first(DEPOT_PATH), "pluto_notebooks")
const default_notebook_dirs = [
default_notebook_dir
]

function new_notebooks_directory()
try
path = joinpath(first(DEPOT_PATH), "pluto_notebooks")
path = default_notebook_dir
if !isdir(path)
mkdir(path)
end
Expand Down
91 changes: 69 additions & 22 deletions src/packages/Packages.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import .ExpressionExplorer: external_package_names
import .PkgCompat
import .PkgCompat: select, is_stdlib
import .PkgCompat: select, is_stdlib, AbstractPackageManagement, FullyManaged, ParentProject, NotManaged

const tiers = [
Pkg.PRESERVE_ALL,
Expand All @@ -13,15 +13,29 @@ const tiers = [
const pkg_token = Token()


function use_plutopkg(topology::NotebookTopology)
!any(values(topology.nodes)) do node
function is_manually_activated(topology::NotebookTopology)
any(values(topology.nodes)) do node
Symbol("Pkg.activate") ∈ node.references ||
Symbol("Pkg.API.activate") ∈ node.references ||
Symbol("Pkg.add") ∈ node.references ||
Symbol("Pkg.API.add") ∈ node.references
end
end

function which_management(topology::NotebookTopology, path::String)
if is_manually_activated(topology)
NotManaged, nothing
else
new_parent_dir = PkgCompat.find_parent_project(path)
if new_parent_dir !== nothing
ParentProject, new_parent_dir
else
FullyManaged, nothing
end
end
end
which_management(notebook::Notebook) = which_management(notebook.topology, notebook.path)

function external_package_names(topology::NotebookTopology)::Set{Symbol}
union!(Set{Symbol}(), external_package_names.(c.module_usings_imports for c in values(topology.codes))...)
end
Expand All @@ -45,17 +59,21 @@ Update the notebook package environment to match the notebook's code. This will:
function sync_nbpkg_core(notebook::Notebook; on_terminal_output::Function=((args...) -> nothing))

👺 = false

new_management_type, new_parent_dir = which_management(notebook)

use_parent_project_old = notebook.nbpkg_ctx isa ParentProject
use_parent_project_new = new_management_type === ParentProject

use_plutopkg_old = notebook.nbpkg_ctx !== nothing
use_plutopkg_new = use_plutopkg(notebook.topology)
use_plutopkg_old = notebook.nbpkg_ctx isa FullyManaged
use_plutopkg_new = new_management_type === FullyManaged

if !use_plutopkg_old && use_plutopkg_new
@info "Started using PlutoPkg!! HELLO reproducibility!"

👺 = true
notebook.nbpkg_ctx = PkgCompat.create_empty_ctx()
end
if use_plutopkg_old && !use_plutopkg_new
notebook.nbpkg_ctx = FullyManaged()
elseif use_plutopkg_old && !use_plutopkg_new
@info "Stopped using PlutoPkg 💔😟😢"

no_packages_loaded_yet = (
Expand All @@ -64,11 +82,32 @@ function sync_nbpkg_core(notebook::Notebook; on_terminal_output::Function=((args
all(PkgCompat.is_stdlib, keys(PkgCompat.project(notebook.nbpkg_ctx).dependencies))
)
👺 = !no_packages_loaded_yet
notebook.nbpkg_ctx = nothing
notebook.nbpkg_ctx = if use_parent_project_new
ParentProject(new_parent_dir)
else
NotManaged()
end
elseif !use_plutopkg_old && !use_plutopkg_new
if use_parent_project_new
if use_parent_project_old
if notebook.nbpkg_ctx.dir != new_parent_dir
👺 = true
notebook.nbpkg_ctx = ParentProject(new_parent_dir)
end
else
👺 = true
notebook.nbpkg_ctx = ParentProject(new_parent_dir)
end
else
if use_parent_project_old
👺 = true
notebook.nbpkg_ctx = NotManaged()
end
end
end


if notebook.nbpkg_ctx !== nothing
if notebook.nbpkg_ctx isa FullyManaged
PkgCompat.mark_original!(notebook.nbpkg_ctx)

old_packages = String.(keys(PkgCompat.project(notebook.nbpkg_ctx).dependencies))
Expand Down Expand Up @@ -101,11 +140,11 @@ function sync_nbpkg_core(notebook::Notebook; on_terminal_output::Function=((args
PkgCompat.withio(notebook.nbpkg_ctx, IOContext(iolistener.buffer, :color => true)) do
withinteractive(false) do
try
Pkg.resolve(notebook.nbpkg_ctx)
Pkg.resolve(notebook.nbpkg_ctx.ctx)
catch e
@warn "Failed to resolve Pkg environment. Removing Manifest and trying again..." exception=e
reset_nbpkg(notebook; keep_project=true, save=false, backup=false)
Pkg.resolve(notebook.nbpkg_ctx)
Pkg.resolve(notebook.nbpkg_ctx.ctx)
end
end
end
Expand All @@ -120,7 +159,7 @@ function sync_nbpkg_core(notebook::Notebook; on_terminal_output::Function=((args
mkeys() = Set(filter(!is_stdlib, [m.name for m in values(PkgCompat.dependencies(notebook.nbpkg_ctx))]))
old_manifest_keys = mkeys()

Pkg.rm(notebook.nbpkg_ctx, [
Pkg.rm(notebook.nbpkg_ctx.ctx, [
Pkg.PackageSpec(name=p)
for p in to_remove
])
Expand Down Expand Up @@ -156,7 +195,7 @@ function sync_nbpkg_core(notebook::Notebook; on_terminal_output::Function=((args
used_tier = tier

try
Pkg.add(notebook.nbpkg_ctx, [
Pkg.add(notebook.nbpkg_ctx.ctx, [
Pkg.PackageSpec(name=p)
for p in to_add
]; preserve=used_tier)
Expand Down Expand Up @@ -313,23 +352,29 @@ end
function reset_nbpkg(notebook::Notebook; keep_project::Bool=false, backup::Bool=true, save::Bool=true)
backup && save && writebackup(notebook)

if notebook.nbpkg_ctx !== nothing
if notebook.nbpkg_ctx isa FullyManaged
p = PkgCompat.project_file(notebook)
m = PkgCompat.manifest_file(notebook)
keep_project || (isfile(p) && rm(p))
isfile(m) && rm(m)

notebook.nbpkg_ctx = PkgCompat.load_ctx(PkgCompat.env_dir(notebook.nbpkg_ctx))
notebook.nbpkg_ctx = FullyManaged(PkgCompat.load_ctx(PkgCompat.env_dir(notebook.nbpkg_ctx)))
else
notebook.nbpkg_ctx = use_plutopkg(notebook.topology) ? PkgCompat.create_empty_ctx() : nothing
new_management_type, new_parent_dir = which_management(notebook)
new_parent_dir = PkgCompat.find_parent_project(notebook.path)
use_parent_project_new = new_parent_dir !== nothing

notebook.nbpkg_ctx = new_management_type === ParentProject ?
ParentProject(new_parent_dir) :
new_management_type()
end

save && save_notebook(notebook)
end

function update_nbpkg_core(notebook::Notebook; level::Pkg.UpgradeLevel=Pkg.UPLEVEL_MAJOR, on_terminal_output::Function=((args...) -> nothing))
if notebook.nbpkg_ctx !== nothing
PkgCompat.mark_original!(notebook.nbpkg_ctx)
if notebook.nbpkg_ctx isa FullyManaged
PkgCompat.mark_original!(notebook.nbpkg_ctx.ctx)

old_packages = String.(keys(PkgCompat.project(notebook.nbpkg_ctx).dependencies))

Expand All @@ -354,11 +399,11 @@ function update_nbpkg_core(notebook::Notebook; level::Pkg.UpgradeLevel=Pkg.UPLEV
PkgCompat.withio(notebook.nbpkg_ctx, IOContext(iolistener.buffer, :color => true)) do
withinteractive(false) do
try
Pkg.resolve(notebook.nbpkg_ctx)
Pkg.resolve(notebook.nbpkg_ctx.ctx)
catch e
@warn "Failed to resolve Pkg environment. Removing Manifest and trying again..." exception=e
reset_nbpkg(notebook; keep_project=true, save=false, backup=false)
Pkg.resolve(notebook.nbpkg_ctx)
Pkg.resolve(notebook.nbpkg_ctx.ctx)
end
end
end
Expand All @@ -372,7 +417,7 @@ function update_nbpkg_core(notebook::Notebook; level::Pkg.UpgradeLevel=Pkg.UPLEV

try
###
Pkg.update(notebook.nbpkg_ctx; level=level)
Pkg.update(notebook.nbpkg_ctx.ctx; level=level)
###
finally
notebook.nbpkg_ctx = PkgCompat.write_auto_compat_entries(notebook.nbpkg_ctx)
Expand Down Expand Up @@ -440,6 +485,8 @@ end
nbpkg_cache(ctx::Union{Nothing,PkgContext}) = ctx === nothing ? Dict{String,String}() : Dict{String,String}(
x => string(PkgCompat.get_manifest_version(ctx, x)) for x in keys(PkgCompat.project(ctx).dependencies)
)
nbpkg_cache(ctx::FullyManaged) = nbpkg_cache(ctx.ctx)
nbpkg_cache(ctx::AbstractPackageManagement) = nbpkg_cache(nothing)

function update_nbpkg_cache!(notebook::Notebook)
notebook.nbpkg_installed_versions_cache = nbpkg_cache(notebook.nbpkg_ctx)
Expand Down
Loading