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

Cannot import and use a dependency #44

Open
ducdetronquito opened this issue Dec 30, 2021 · 12 comments
Open

Cannot import and use a dependency #44

ducdetronquito opened this issue Dec 30, 2021 · 12 comments
Labels
question Further information is requested

Comments

@ducdetronquito
Copy link

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 :)

@xyaman
Copy link

xyaman commented Dec 30, 2021

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

@nektro
Copy link
Owner

nektro commented Dec 30, 2021

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 :)

@ducdetronquito
Copy link
Author

ducdetronquito commented Dec 31, 2021

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
Copy link
Author

ducdetronquito commented Dec 31, 2021

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 ?

@nektro
Copy link
Owner

nektro commented Dec 31, 2021

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:

@ducdetronquito
Copy link
Author

ducdetronquito commented Jan 1, 2022

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.

@nektro
Copy link
Owner

nektro commented Jan 2, 2022

do you have a copy of your build.zig?

@nektro
Copy link
Owner

nektro commented Jan 2, 2022

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

@ducdetronquito
Copy link
Author

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 :)

@nektro nektro added the question Further information is requested label Jan 6, 2022
@nektro
Copy link
Owner

nektro commented Jan 18, 2022

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

@ducdetronquito
Copy link
Author

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 !

@sakateka
Copy link

sakateka commented Feb 15, 2022

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-};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants