authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-04 23:17:38-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-04 23:17:38-04:00
logb35c74ea4c9d93d6a8d90812d2066a78b9abb64e
tree2bd1b9b8f353718c385fd6b800c1b7372840f99f
parent2bf1b6840d33a27614630ddb34f53a859fc87345
signaturelock-open Commit is signed but in an unrecognized format.

stage1: use os_path_resolve instead of os_path_real

to canonicalize imports. This means that softlinks can represent different files, but referencing the same absolute path different ways still references the same import.

3 files changed, 30 insertions(+), 44 deletions(-)

src/analyze.cpp+6-6
......@@ -4236,9 +4236,9 @@ void preview_use_decl(CodeGen *g, AstNode *node) {
42364236 node->data.use.value = result;
42374237}
42384238
4239ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *abs_full_path, Buf *source_code) {
4239ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *resolved_path, Buf *source_code) {
42404240 if (g->verbose_tokenize) {
4241 fprintf(stderr, "\nOriginal Source (%s):\n", buf_ptr(abs_full_path));
4241 fprintf(stderr, "\nOriginal Source (%s):\n", buf_ptr(resolved_path));
42424242 fprintf(stderr, "----------------\n");
42434243 fprintf(stderr, "%s\n", buf_ptr(source_code));
42444244
......@@ -4250,7 +4250,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *a
42504250 tokenize(source_code, &tokenization);
42514251
42524252 if (tokenization.err) {
4253 ErrorMsg *err = err_msg_create_with_line(abs_full_path, tokenization.err_line, tokenization.err_column,
4253 ErrorMsg *err = err_msg_create_with_line(resolved_path, tokenization.err_line, tokenization.err_column,
42544254 source_code, tokenization.line_offsets, tokenization.err);
42554255
42564256 print_err_msg(err, g->err_color);
......@@ -4268,7 +4268,7 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *a
42684268 import_entry->package = package;
42694269 import_entry->source_code = source_code;
42704270 import_entry->line_offsets = tokenization.line_offsets;
4271 import_entry->path = abs_full_path;
4271 import_entry->path = resolved_path;
42724272
42734273 import_entry->root = ast_parse(source_code, tokenization.tokens, import_entry, g->err_color);
42744274 assert(import_entry->root);
......@@ -4278,10 +4278,10 @@ ImportTableEntry *add_source_file(CodeGen *g, PackageTableEntry *package, Buf *a
42784278
42794279 Buf *src_dirname = buf_alloc();
42804280 Buf *src_basename = buf_alloc();
4281 os_path_split(abs_full_path, src_dirname, src_basename);
4281 os_path_split(resolved_path, src_dirname, src_basename);
42824282
42834283 import_entry->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));
4284 g->import_table.put(abs_full_path, import_entry);
4284 g->import_table.put(resolved_path, import_entry);
42854285 g->import_queue.append(import_entry);
42864286
42874287 import_entry->decls_scope = create_decls_scope(import_entry->root, nullptr, nullptr, import_entry);
src/codegen.cpp+16-21
......@@ -6866,19 +6866,16 @@ static void define_builtin_compile_vars(CodeGen *g) {
68666866 ensure_cache_dir(g);
68676867 os_write_file(builtin_zig_path, contents);
68686868
6869 int err;
6870 Buf *abs_full_path = buf_alloc();
6871 if ((err = os_path_real(builtin_zig_path, abs_full_path))) {
6872 fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(builtin_zig_path), err_str(err));
6873 exit(1);
6874 }
6869 Buf *resolved_path = buf_alloc();
6870 Buf *resolve_paths[] = {builtin_zig_path};
6871 *resolved_path = os_path_resolve(resolve_paths, 1);
68756872
68766873 assert(g->root_package);
68776874 assert(g->std_package);
68786875 g->compile_var_package = new_package(buf_ptr(&g->cache_dir), builtin_zig_basename);
68796876 g->root_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);
68806877 g->std_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);
6881 g->compile_var_import = add_source_file(g, g->compile_var_package, abs_full_path, contents);
6878 g->compile_var_import = add_source_file(g, g->compile_var_package, resolved_path, contents);
68826879 scan_import(g, g->compile_var_import);
68836880}
68846881
......@@ -7034,17 +7031,17 @@ static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package
70347031 Buf *code_basename = buf_create_from_str(basename);
70357032 Buf path_to_code_src = BUF_INIT;
70367033 os_path_join(g->zig_std_special_dir, code_basename, &path_to_code_src);
7037 Buf *abs_full_path = buf_alloc();
7038 int err;
7039 if ((err = os_path_real(&path_to_code_src, abs_full_path))) {
7040 zig_panic("unable to open '%s': %s\n", buf_ptr(&path_to_code_src), err_str(err));
7041 }
7034
7035 Buf *resolve_paths[] = {&path_to_code_src};
7036 Buf *resolved_path = buf_alloc();
7037 *resolved_path = os_path_resolve(resolve_paths, 1);
70427038 Buf *import_code = buf_alloc();
7043 if ((err = os_fetch_file_path(abs_full_path, import_code, false))) {
7039 int err;
7040 if ((err = os_fetch_file_path(resolved_path, import_code, false))) {
70447041 zig_panic("unable to open '%s': %s\n", buf_ptr(&path_to_code_src), err_str(err));
70457042 }
70467043
7047 return add_source_file(g, package, abs_full_path, import_code);
7044 return add_source_file(g, package, resolved_path, import_code);
70487045}
70497046
70507047static PackageTableEntry *create_bootstrap_pkg(CodeGen *g, PackageTableEntry *pkg_with_main) {
......@@ -7122,20 +7119,18 @@ static void gen_root_source(CodeGen *g) {
71227119 Buf *rel_full_path = buf_alloc();
71237120 os_path_join(&g->root_package->root_src_dir, &g->root_package->root_src_path, rel_full_path);
71247121
7125 Buf *abs_full_path = buf_alloc();
7126 int err;
7127 if ((err = os_path_real(rel_full_path, abs_full_path))) {
7128 fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(rel_full_path), err_str(err));
7129 exit(1);
7130 }
7122 Buf *resolved_path = buf_alloc();
7123 Buf *resolve_paths[] = {rel_full_path};
7124 *resolved_path = os_path_resolve(resolve_paths, 1);
71317125
71327126 Buf *source_code = buf_alloc();
7127 int err;
71337128 if ((err = os_fetch_file_path(rel_full_path, source_code, true))) {
71347129 fprintf(stderr, "unable to open '%s': %s\n", buf_ptr(rel_full_path), err_str(err));
71357130 exit(1);
71367131 }
71377132
7138 g->root_import = add_source_file(g, g->root_package, abs_full_path, source_code);
7133 g->root_import = add_source_file(g, g->root_package, resolved_path, source_code);
71397134
71407135 assert(g->root_out_name);
71417136 assert(g->out_type != OutTypeUnknown);
src/ir.cpp+8-17
......@@ -16131,29 +16131,20 @@ static TypeTableEntry *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructi
1613116131 os_path_join(search_dir, import_target_path, &full_path);
1613216132
1613316133 Buf *import_code = buf_alloc();
16134 Buf *abs_full_path = buf_alloc();
16135 int err;
16136 if ((err = os_path_real(&full_path, abs_full_path))) {
16137 if (err == ErrorFileNotFound) {
16138 ir_add_error_node(ira, source_node,
16139 buf_sprintf("unable to find '%s'", buf_ptr(import_target_path)));
16140 return ira->codegen->builtin_types.entry_invalid;
16141 } else {
16142 ira->codegen->error_during_imports = true;
16143 ir_add_error_node(ira, source_node,
16144 buf_sprintf("unable to open '%s': %s", buf_ptr(&full_path), err_str(err)));
16145 return ira->codegen->builtin_types.entry_invalid;
16146 }
16147 }
16134 Buf *resolved_path = buf_alloc();
16135
16136 Buf *resolve_paths[] = { &full_path, };
16137 *resolved_path = os_path_resolve(resolve_paths, 1);
1614816138
16149 auto import_entry = ira->codegen->import_table.maybe_get(abs_full_path);
16139 auto import_entry = ira->codegen->import_table.maybe_get(resolved_path);
1615016140 if (import_entry) {
1615116141 ConstExprValue *out_val = ir_build_const_from(ira, &import_instruction->base);
1615216142 out_val->data.x_import = import_entry->value;
1615316143 return ira->codegen->builtin_types.entry_namespace;
1615416144 }
1615516145
16156 if ((err = os_fetch_file_path(abs_full_path, import_code, true))) {
16146 int err;
16147 if ((err = os_fetch_file_path(resolved_path, import_code, true))) {
1615716148 if (err == ErrorFileNotFound) {
1615816149 ir_add_error_node(ira, source_node,
1615916150 buf_sprintf("unable to find '%s'", buf_ptr(import_target_path)));
......@@ -16164,7 +16155,7 @@ static TypeTableEntry *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructi
1616416155 return ira->codegen->builtin_types.entry_invalid;
1616516156 }
1616616157 }
16167 ImportTableEntry *target_import = add_source_file(ira->codegen, target_package, abs_full_path, import_code);
16158 ImportTableEntry *target_import = add_source_file(ira->codegen, target_package, resolved_path, import_code);
1616816159
1616916160 scan_import(ira->codegen, target_import);
1617016161