From 67b4de33d2729fdb21337e5a0e05f6273bce23ba Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 2 Mar 2019 10:38:27 -0500 Subject: compile error for import outside package path closes #2024 there's a new cli option `--main-pkg-path` which you can use to choose a different root package directory besides the one inferred from the root source file and a corresponding build.zig API: foo.setMainPkgPath(path) --- src/ir.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index 7c45a9d588..2452011d79 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -17026,6 +17026,18 @@ static IrInstruction *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructio return ir_const_type(ira, &import_instruction->base, import_entry->value); } + if (source_kind == SourceKindNonRoot) { + ZigPackage *cur_scope_pkg = scope_package(import_instruction->base.scope); + Buf *pkg_root_src_dir = &cur_scope_pkg->root_src_dir; + Buf resolved_root_src_dir = os_path_resolve(&pkg_root_src_dir, 1); + if (!buf_starts_with_buf(resolved_path, &resolved_root_src_dir)) { + ir_add_error_node(ira, source_node, + buf_sprintf("import of file outside package path: '%s'", + buf_ptr(import_target_path))); + return ira->codegen->invalid_instruction; + } + } + if ((err = file_fetch(ira->codegen, resolved_path, import_code))) { if (err == ErrorFileNotFound) { ir_add_error_node(ira, source_node, -- cgit v1.2.3