zigmod icon indicating copy to clipboard operation
zigmod copied to clipboard

Cannot import and use a dependency

Open ducdetronquito opened this issue 4 years ago • 12 comments

Hi @nektro ,

I think I encountered a bug or it can be just be me misusing zigmod !

Steps to reproduce: I created a small library zigmod_dep that provide an add function. (To create the library, I juste used zig init-lib and zigmod init)

I created an other library zigmod_base that uses zigmod_dep.add. (To create the library, I juste used zig init-lib and zigmod init)

When running the tests of zigmod_base, it does not find the zigmod_dep package and crashes with the following error:

λ zig build test
.\src\main.zig:3:20: error: unable to find 'zigmod_dep'
const zigmod_dep = @import("zigmod_dep");
                   ^
.\src\main.zig:6:12: note: referenced here
    return zigmod_dep.add(a, a);
           ^
The following command exited with error code 1:
C:\Program Files (x86)\Zig\0.9.0\zig.exe test C:\Users\DEV\Documents\Code\tests\zigmod_base\src\main.zig --cache-dir C:\Users\DEV\Documents\Code\tests\zigmod_base\zig-cache --global-cache-dir C:\Users\DEV\AppData\Local\zig --name test --pkg-begin zigmod_base C:\Users\DEV\Documents\Code\tests\zigmod_base\src\main.zig --pkg-begin zigmod_dep C:\Users\DEV\Documents\Code\tests\zigmod_base\.zigmod\deps\git\github.com\ducdetronquito\zigmod_dep\src\main.zig --pkg-end --pkg-end

error: the following build command failed with exit code 1:
C:\Users\DEV\Documents\Code\tests\zigmod_base\zig-cache\o\8f0600972eaf8c537216c8b7e5934962\build.exe C:\Program Files (x86)\Zig\0.9.0\zig.exe C:\Users\DEV\Documents\Code\tests\zigmod_base C:\Users\DEV\Documents\Code\tests\zigmod_base\zig-cache C:\Users\DEV\AppData\Local\zig test

To fix the error, and be able to compile I just patched deps.zig in zigmod_base as such:


// Before
pub const packages = &[_]Package{
    package_data._muh5sqkmx04g,
};

// After
pub const packages = &[_]Package{
    package_data._muh5sqkmx04g,
    package_data._cyghmh8qzz6y,
};

I hope I have clear enough to help you reproduce it :)

ducdetronquito avatar Dec 30 '21 10:12 ducdetronquito

I have the same error and patching deps.zig following @ducdetronquito step fixed it.

xyaman avatar Dec 30 '21 12:12 xyaman

in zigmod_base, this line and this line should not point to the same file. the latter is for the use case of making a "library" in the sense a package that others may depend on. thus you may depend on the internal package yourself by doing @import("zigmod_base").

for your use case however it appears, given the edit you made, that what you want is for this line to be removed and this line be changed to say root_dependencies:. after those changes rerunning zigmod fetch should give you the deps.zig you desire. hope this helps :)

nektro avatar Dec 30 '21 16:12 nektro

Hi @nektro !

I don't understand you first paragraph, because in this case both zigmod_base and zigmod_dep are libraries.

I will try to describe my actual situation and not an example, maybe it will be more clear !

I edited the zig.mod files of each libraries following your advice of the second paragraph. (I read the doc but I still don't understand the difference between a dependency and a root_dependency :/) As a result, I can run tests (zig build test) for h11 but I can't for requestz because the compiler does not find the h11 package.

Here is the zig.mod files of each libraries:

# http zig.mod
id: a2vh8na282lel0as4l57ibmjje9oqt2o2fajm5hofpsbqoa8
name: http
main: src/main.zig
license: 0BSD
description: HTTP core types for Zig 🦴
dependencies:
# h11 zig.mod
id: 7noqknvqkyyup91axamyzfiet5r41k9apsb9wzbn9ay3l2jc
name: h11
license: 0BSD
description: I/O agnostic HTTP/1.1 implementation for Zig 🦎
root_dependencies:
  - src: git https://github.com/ducdetronquito/http branch-release/0.2.0
# requestz zig.mod
id: 86gq5kz04nyy4e7eq2njytvqivd2unre6i965ol3p5gz7cw3
name: requestz
license: 0BSD
description: HTTP client for Zig 🦎
root_dependencies:
   - src: git https://github.com/nektro/iguanaTLS commit-d49c382
   - src: git https://github.com/MasterQ32/zig-network commit-b9c5282
   - src: git https://github.com/ducdetronquito/http branch-release/0.2.0
   - src: git https://github.com/ducdetronquito/h11 branch-release/0.2.0

What am I missing ^^ ?

Thanks in advance, and have a nice day !

ducdetronquito avatar Dec 31 '21 09:12 ducdetronquito

After toying a bit, here are the zig.mod files that make each library importable from another library along with its dependencies, and being able to run tests on each library.

# http zig.mod
id: a2vh8na282lel0as4l57ibmjje9oqt2o2fajm5hofpsbqoa8
name: http
main: src/main.zig
license: 0BSD
description: HTTP core types for Zig 🦴
dependencies:
# h11 zig.mod
id: 7noqknvqkyyup91axamyzfiet5r41k9apsb9wzbn9ay3l2jc
name: h11
license: 0BSD
description: I/O agnostic HTTP/1.1 implementation for Zig 🦎
root_dependencies:
  - src: git https://github.com/ducdetronquito/http branch-release/0.2.0
dependencies:
  - src: git https://github.com/ducdetronquito/http branch-release/0.2.0
# requestz zig.mod
id: 86gq5kz04nyy4e7eq2njytvqivd2unre6i965ol3p5gz7cw3
name: requestz
license: 0BSD
description: HTTP client for Zig 🦎
root_dependencies:
   - src: git https://github.com/nektro/iguanaTLS commit-d49c382
   - src: git https://github.com/MasterQ32/zig-network commit-b9c5282
   - src: git https://github.com/ducdetronquito/http branch-release/0.2.0
   - src: git https://github.com/ducdetronquito/h11 branch-release/0.2.0
dependencies:
   - src: git https://github.com/nektro/iguanaTLS commit-d49c382
   - src: git https://github.com/MasterQ32/zig-network commit-b9c5282
   - src: git https://github.com/ducdetronquito/http branch-release/0.2.0
   - src: git https://github.com/ducdetronquito/h11 branch-release/0.2.0

Could you explain me why I must define both dependencies and root_dependencies ?

ducdetronquito avatar Dec 31 '21 09:12 ducdetronquito

so since all of the manifests you described are libraries, you want to set main: to be the entry point of the package, and remove root_dependencies:

then in any testing code you import the package by the name you set in name:

nektro avatar Dec 31 '21 18:12 nektro

Unfortunately, doing that put me into the same situation described in my first comment of this issue: importing a dependency by name crashes when running zig build test.

ducdetronquito avatar Jan 01 '22 11:01 ducdetronquito

do you have a copy of your build.zig?

nektro avatar Jan 02 '22 02:01 nektro

https://github.com/nektro/zig-double this is an example repo that shows off the structure i explained above

nektro avatar Jan 02 '22 04:01 nektro

https://github.com/nektro/zig-double this is an example repo that shows off the structure i explained above

Thanks I will have a look at it :)

ducdetronquito avatar Jan 02 '22 09:01 ducdetronquito

how'd it go? (happy to chat on discord too)

nektro avatar Jan 18 '22 10:01 nektro

Hi @nektro

Not very far as I have not much time to work on Zig these days... I keep you in touch when I will work on it again !

ducdetronquito avatar Jan 18 '22 10:01 ducdetronquito

It seems that the use of the term "root_dependencies" is not very intuitive, and its difference with "dependencies" is not clearly described in the documentation :-(

My misunderstanding of root_dependencies

Hi @nektro! I think I reproduced the same bug. I have a very simple program https://github.com/sakateka/psm, and I needed to format the time in it. I decided to try zigmod (download the latest release because the build is falling) and zig-time. I went through the tutorial, added the necessary dependency, made a zigmod fetch, tried to build and got an error

./src/main.zig:3:14: error: unable to find 'time'
const time = @import("time");
             ^
psm...The following command exited with error code 1:
/home/user/zig/zig-linux-x86_64-0.10.0-dev.745+c6cd919a1/zig build-exe /home/user/nohup/psm/src/main.zig --cache-dir /home/user/nohup/psm/zig-cache --global-cache-dir /home/user/.cache/zig --name psm --enable-cache
error: the following build command failed with exit code 1:
/home/user/nohup/psm/zig-cache/o/30ee4348280de942d92a0553f7eff9cb/build /home/user/zig/zig-linux-x86_64-0.10.0-dev.745+c6cd919a1/zig /home/user/nohup/psm /home/user/nohup/psm/zig-cache /home/user/.cache/zig

It looks like in deps.zig the necessary packages were not added

rg -A2 'const packages' deps.zig
75:pub const packages = &[_]Package{
76-};
77-

After manual editing of deps.zig, the build was successful

 rg -A2 'const packages' deps.zig
75:pub const packages = &[_]Package{
76-    package_data._iecwp4b3bsfm,
77-};

sakateka avatar Feb 15 '22 17:02 sakateka