Jump to page: 1 2 3
Thread overview
Keyword "package" prevents from importing a package module "package.d"
Nov 02
BoQsc
Nov 02
BoQsc
Nov 02
Arafel
Nov 02
BoQsc
Nov 02
BoQsc
Nov 02
Arafel
Nov 02
BoQsc
Nov 02
Arafel
Nov 02
Arafel
November 02

Source File package.d is a package module which contains import statements to import other modules.

How would one import a package.d module when keyword "package" is preventing that?

Example:

import package;

Error:

datatypes.d(1): Error: identifier expected following `import`
datatypes.d(1): Error: `;` expected

Extensive run.dlang.io example

Expected correct result if package.d was possible to import:

waffles/program.d(6): Error: `num` matches conflicting symbols:
waffles/testing1.d(1):        variable `testing1.num`
waffles/testing2.d(1):        variable `testing2.num`
November 02

Edit incorrect link to example:

>

Extensive run.dlang.io example

Correct link:
https://run.dlang.io/is/Zbrn75

November 02
On 02.11.23 11:45, BoQsc wrote:
> Edit incorrect link to example:
>> #### [Extensive run.dlang.io example](https://run.dlang.io/is/f3jURn)
> 
> Correct link:
> https://run.dlang.io/is/Zbrn75
> 

```
--- waffles/program.d
import waffles;
```

See https://dlang.org/spec/module.html#package-module
November 02

On Thursday, 2 November 2023 at 10:53:12 UTC, Arafel wrote:

>

On 02.11.23 11:45, BoQsc wrote:

>

Edit incorrect link to example:

>

Extensive run.dlang.io example

Correct link:
https://run.dlang.io/is/Zbrn75

--- waffles/program.d
import waffles;

See https://dlang.org/spec/module.html#package-module

Weirdly enough it does not work on Windows operating system.

program.d(1): Error: unable to read module `waffles`
program.d(1):        Expected 'waffles.d' or 'waffles\package.d' in one of the following import paths:
import path[0] = .
import path[1] = C:\D\dmd2\windows\bin\..\..\src\phobos
import path[2] = C:\D\dmd2\windows\bin\..\..\src\druntime\import
Failed: ["C:\\D\\dmd2\\windows\\bin\\dmd.exe", "-v", "-o-", "program.d", "-I."]

C:\waffles>dmd -i -run program.d
program.d(1): Error: unable to read module `waffles`
program.d(1):        Expected 'waffles.d' or 'waffles\package.d' in one of the following import paths:
import path[0] = C:\D\dmd2\windows\bin64\..\..\src\phobos
import path[1] = C:\D\dmd2\windows\bin64\..\..\src\druntime\import

November 02

On Thursday, 2 November 2023 at 11:12:58 UTC, BoQsc wrote:

>

On Thursday, 2 November 2023 at 10:53:12 UTC, Arafel wrote:

>

On 02.11.23 11:45, BoQsc wrote:

>

Edit incorrect link to example:

>

Extensive run.dlang.io example

Correct link:
https://run.dlang.io/is/Zbrn75

--- waffles/program.d
import waffles;

void main()
{
    import std;

    writeln(num);
}

--- waffles/package.d
module waffles;

public import waffles.testing1;
public import waffles.testing2;

--- waffles/testing1.d
int num = 5;

--- waffles/testing2.d
int num2 = 9;
November 02

On Thursday, 2 November 2023 at 11:32:40 UTC, Imperatorn wrote:

>

On Thursday, 2 November 2023 at 11:12:58 UTC, BoQsc wrote:

>

On Thursday, 2 November 2023 at 10:53:12 UTC, Arafel wrote:

>

On 02.11.23 11:45, BoQsc wrote:

>

Edit incorrect link to example:

>

Extensive run.dlang.io example

Correct link:
https://run.dlang.io/is/Zbrn75

--- waffles/program.d
import waffles;

void main()
{
    import std;

    writeln(num);
}

--- waffles/package.d
module waffles;

public import waffles.testing1;
public import waffles.testing2;

--- waffles/testing1.d
int num = 5;

--- waffles/testing2.d
int num2 = 9;

num and num2 was never a problem.

The current major problem is that it does not work on Windows operating system with either rdmd or dmd. While it does work on run.dlang.io.

November 02

On Thursday, 2 November 2023 at 11:57:57 UTC, BoQsc wrote:

>

On Thursday, 2 November 2023 at 11:32:40 UTC, Imperatorn wrote:

>

On Thursday, 2 November 2023 at 11:12:58 UTC, BoQsc wrote:

>

On Thursday, 2 November 2023 at 10:53:12 UTC, Arafel wrote:

>

On 02.11.23 11:45, BoQsc wrote:

>

[...]

--- waffles/program.d
import waffles;

void main()
{
    import std;

    writeln(num);
}

--- waffles/package.d
module waffles;

public import waffles.testing1;
public import waffles.testing2;

--- waffles/testing1.d
int num = 5;

--- waffles/testing2.d
int num2 = 9;

num and num2 was never a problem.

The current major problem is that it does not work on Windows operating system with either rdmd or dmd. While it does work on run.dlang.io.

That's not all I changed. It works for me on Windows.

November 02

On Thursday, 2 November 2023 at 11:12:58 UTC, BoQsc wrote:

>

Weirdly enough it does not work on Windows operating system.
[...]

program.d(1): Error: unable to read module `waffles`
program.d(1):        Expected 'waffles.d' or 'waffles\package.d' in one of the following import paths:
import path[0] = .
import path[1] = C:\D\dmd2\windows\bin\..\..\src\phobos
import path[2] = C:\D\dmd2\windows\bin\..\..\src\druntime\import
Failed: ["C:\\D\\dmd2\\windows\\bin\\dmd.exe", "-v", "-o-", "program.d", "-I."]

C:\waffles>dmd -i -run program.d
program.d(1): Error: unable to read module `waffles`
program.d(1):        Expected 'waffles.d' or 'waffles\package.d' in one of the following import paths:
import path[0] = C:\D\dmd2\windows\bin64\..\..\src\phobos
import path[1] = C:\D\dmd2\windows\bin64\..\..\src\druntime\import

The problem is the directory you're running the command from. You need to run dmd from the parent directory of waffles.

November 02
On 02.11.23 12:57, BoQsc wrote:
> The current major problem is that it does not work on Windows operating system with either `rdmd` or `dmd`. While it does work on run.dlang.io.

The problem is with your import path.

If you say:

```d
import waffles;
```

The compiler would search for either `waffles.d` or `waffles/package.d` **in your current working directory**.

So you have three options:

1. You can compile from the parent directory, most likely what run.dlang.io does: `dmd -i -run waffles/program.d`
2. You can explicitly add all the files to the dmd invocation (I think this is what dub does), although that likely defeats the purpose of `rdmd` and `dmd -i`.
3. You can add `..` (the parent directory) to your search path: `dmd -I.. [......]`

Actually, the cleanest (and in my view proper) solution would be to create a new `waffles` directory with the "package" itself, and take the main function out of it, so you'd have:

```
waffles
|
+-- program.d
|
+-- waffles
    |
    +-- package.d
    |
    +-- testing1.d
    |
    +-- testing2.d
```
November 02

Well the whole thread is about importing package.d while being inside package to provide runnable working example which contains debug information of the package.

Sidenote:
This is essentially useful when distributing over many machines/platforms via dub package manager.

You would want to have a source file inside the project that imports package.d and is runnable with well tested example behaviour.

Therefore the need to import package.d is needed and I can't see a solution, which means
that D Language might have to introduce a way to import package.d from inside the package, if there is a need to further improve experience of having a self-testing packages in both dub and package modules.

« First   ‹ Prev
1 2 3