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

[BUG] Package extension.yml file #86

Closed
joshpalis opened this issue Aug 11, 2022 · 3 comments
Closed

[BUG] Package extension.yml file #86

joshpalis opened this issue Aug 11, 2022 · 3 comments
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@joshpalis
Copy link
Member

joshpalis commented Aug 11, 2022

What is the bug?

Building and running the opensearch-sdk artifact would look for the extension.yml file within the opensearch-sdk/build/distributions/opensearch-sdk-1.0.0-SNAPSHOT/src/test/resources/ directory instead of the opensearch-sdk/src/test/resources directory

How can one reproduce the bug?

Run ./gradlew build in the opensearch-sdk directory to produce the artifact. Navigate to opensearch-sdk/build/distributions/ and untar the generated artifact using tar -xvf opensearch-sdk-1.0.0-SNAPSHOT.tar. Navigate to opensearch-sdk-1.0.0-SNAPSHOT/ and run ./bin/opensearch-sdk

This will produce the following error :
Exception in thread "main" java.io.FileNotFoundException: src/test/resources/extension.yml (No such file or directory)

In order to fix this issue, we must create the directory within the package and generate the extension.yml file here so that the artifact has access.

EDIT: Following discussion, the extensions.yml file will be moved to the config directory where opensearch.yml is currently stored.

@joshpalis joshpalis added bug Something isn't working untriaged labels Aug 11, 2022
@joshpalis joshpalis changed the title [BUG] Package Extension.yml file [BUG] Package extension.yml file Aug 11, 2022
@minalsha minalsha added good first issue Good for newcomers and removed untriaged labels Aug 12, 2022
@dbwiddis
Copy link
Member

dbwiddis commented Aug 16, 2022

This is not as simple as just changing the directory paths.

Both "file paths" are accessible with the current setup but neither file path will work in a compiled jar file.

The file should be moved to src/main/resources which will put it at the root of the classpath. You'll then have to use some variant of getResource() or getResourceAsStream() from the classloader, such as:

// reads from the root of the classpath so will find something in src/main/resources
InputStream in = this.getClass().getClassLoader().getResourceAsStream("extension.yml");
// or get the filepath
String ymlPath = this.getClass().getClassLoader().getResource("extension.yml").getPath();

You can also get it from the class itself using NIO, but this is relative to the package, so if you're in the org.opensearch.sdk package you need to navigate up 3 directories:

// reads from the root of the package
Path ymlPath = Paths.get(this.getClass().getResource("../../../extension.yml").toURI());
// or
File ymlFile = new File(this.getClass().getResource("../../../extension.yml").toURI());
// I think if you lead with a slash you can work around the relative package directory things, so try
Path ymlPath = Paths.get(this.getClass().getResource("/extension.yml").toURI());
// or
String ymlPath = this.getClass().getResource("../../config/dev/example.xml").getPath();

or variations of the above.

One other complication for using the classpath: anyone else can have a same-named file on the class path and they will conflict. So a generic name like extension.yml is going to be problematic. To work around this:

  • put it in a subdirectory in src/main/resources that a user is unlikely to also have in their own classpath, e.g., src/main/resources/org/opensearch/sdk (which is essentially like putting it in the package anyway!)
  • give it a more verbose name like opensearch_sdk_extension.yml

@dbwiddis
Copy link
Member

See this commit for implementation for a different YAML file: 821d27f

@dbwiddis
Copy link
Member

The changes in #109 may make this obsolete. Review discussion on that PR before spending any time on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants