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] Bundle() does not restore CWD call when baseDir is used #166

Closed
2 tasks done
g-radam opened this issue May 23, 2024 · 5 comments · Fixed by #167
Closed
2 tasks done

[BUG] Bundle() does not restore CWD call when baseDir is used #166

g-radam opened this issue May 23, 2024 · 5 comments · Fixed by #167
Labels
bug Something isn't working

Comments

@g-radam
Copy link

g-radam commented May 23, 2024

Describe the bug.

Invoking the bundle() function with a baseDir option provided will have the working directory changed, but will not be changed back on function exit. Successive calls to this method where each call provides a relative path baseDir option will fail, specifically on the 2nd call, since the original relative path no longer makes sense in the CWD.

Error:

Error: ENOENT: no such file or directory, chdir '<project>/src' -> './src'
    at process.wrappedChdir (node:internal/bootstrap/switches/does_own_process_state:130:14)
    at process.chdir (<project>/node_modules/graceful-fs/polyfills.js:22:11)
    at bundle (<project>/node_modules/@asyncapi/bundler/lib/index.js:85:17)
    at build (<project>/gulpfile.js:37:30)

The lines in question are:

The function will need to copy the CWD, and restore it prior to exiting. In cases where the function body throws, the exception should most likely be caught, the CWD reverted, and the exception be propagated as per normal.

Expected behavior

Invoking the bundle restores the CWD.

Screenshots

N/A

How to Reproduce

  // Bundle server AsyncAPI files
  const serverMerged = await bundle(["common.yaml"], {
    base: "server.yaml",
    baseDir: "./src/",
    xOrigin: false,
  });

  // Merge client AsyncAPI files
  // Fails here due to ./src/ relative path no longer correct, as the cwd IS NOW ./src/. 
  const clientMerged = await bundle(["common.yaml"], {
    base: "client.yaml",
    baseDir: "./src/",
    xOrigin: false,
  });

🥦 Browser

None

👀 Have you checked for similar open issues?

  • I checked and didn't find similar issue

🏢 Have you read the Contributing Guidelines?

Are you willing to work on this issue ?

Yes I am willing to submit a PR!

@g-radam g-radam added the bug Something isn't working label May 23, 2024
Copy link

Welcome to AsyncAPI. Thanks a lot for reporting your first issue. Please check out our contributors guide and the instructions about a basic recommended setup useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@aeworxet
Copy link
Collaborator

Please copy&paste this compiled index.js to your index.js and check if it solves the issue.

index.js
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.originDir = void 0;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const util_1 = require("./util");
const document_1 = require("./document");
const parser_1 = require("./parser");
exports.originDir = String(process.env.PWD);
async function bundle(files, options = {}) {
    // if one string was passed, convert it to an array
    if (typeof files === 'string') {
        files = Array.from(files.split(' '));
    }
    if (options.baseDir && typeof options.baseDir === 'string') {
        process.chdir(path_1.default.resolve(exports.originDir, options.baseDir));
    }
    else if (options.baseDir && Array.isArray(options.baseDir)) {
        process.chdir(path_1.default.resolve(exports.originDir, String(options.baseDir[0]))); // guard against passing an array
    }
    const readFiles = files.map(file => (0, fs_1.readFileSync)(file, 'utf-8')); // eslint-disable-line
    const parsedJsons = readFiles.map(file => (0, util_1.toJS)(file));
    const majorVersion = (0, util_1.versionCheck)(parsedJsons);
    if (typeof options.base !== 'undefined') {
        if (typeof options.base === 'string') {
            options.base = (0, fs_1.readFileSync)(options.base, 'utf-8'); // eslint-disable-line
        }
        else if (Array.isArray(options.base)) {
            options.base = (0, fs_1.readFileSync)(String(options.base[0]), 'utf-8'); // eslint-disable-line
        }
        options.base = (0, util_1.toJS)(options.base);
        await (0, parser_1.parse)(options.base, majorVersion, options);
    }
    const resolvedJsons = await (0, util_1.resolve)(parsedJsons, majorVersion, options);
    process.chdir(exports.originDir);
    return new document_1.Document(resolvedJsons, options.base);
}
exports.default = bundle;
// 'module.exports' is added to maintain backward compatibility with Node.js
// projects, that use CJS module system.
module.exports = bundle;

@g-radam
Copy link
Author

g-radam commented May 24, 2024

I can confirm that worked :)

@aeworxet
Copy link
Collaborator

@g-radam
This change is included in v0.5.2. Please check once again with the published version.

@g-radam
Copy link
Author

g-radam commented Jun 7, 2024

@aeworxet I've validated that the issue is fixed in v0.5.2! :) Many thanks aeworxet!

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

Successfully merging a pull request may close this issue.

2 participants