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

Stack traces loaded through require() always show duk_bi_global.c as source file #58

Closed
emil-e opened this issue Oct 29, 2014 · 12 comments
Labels
Milestone

Comments

@emil-e
Copy link

emil-e commented Oct 29, 2014

duk_bi_global_object_require uses the function duk_eval (duk_bi_global.c:1079) to compile the source returned by the modSearch function. Since duk_eval is defined as,

#define duk_eval(ctx)  \
    ((void) duk_push_string((ctx), __FILE__), \
    (void) duk_eval_raw((ctx), NULL, 0, DUK_COMPILE_EVAL))

the stack frames for functions loaded through require(...) will show the source file as duk_bi_global.c.

I came up with the following fix (replace duk_bi_global.c:1079) but I don't know if this is the "right" solution or not:

duk_dup(ctx, 3);
duk_eval_raw(ctx, NULL, 0, DUK_COMPILE_EVAL);

This uses the resolved module ID as the filename for duk_eval_raw instead.

@andoma
Copy link
Contributor

andoma commented Oct 29, 2014

This is definitely an improvement over the current solution which results in very confusing backtraces.

@svaarala
Copy link
Owner

Looks definitely like something to fix, thanks for reporting it.

@svaarala svaarala added this to the v1.1.0 milestone Oct 29, 2014
@svaarala
Copy link
Owner

Technically it might be useful if the modSearch() function could provide the filename if the file actually gets loaded from a file system (not always the case but quite common).

Unfortunately this isn't easily fixable for 1.x because but we can look into changing the contract (@creationix had also some issues with the current one, specifically re: giving a fully resolved ID to modSearch) in 2.0.

@svaarala
Copy link
Owner

Ah, I see. The duk_bi_global_object_require() already does the following which spot fixes the fileName for the module function itself, but that of course doesn't get "inherited" into functions defined within the module:

        /* Finish the wrapped module source. */
        duk_push_string(ctx, "})");
        duk_concat(ctx, 3);
        duk_eval(ctx);

        /* Force 'fileName' property of the module function so that if the
         * module creates a logger, the logger name defaults to the module
         * name.
         */
        duk_dup(ctx, 3);
        duk_put_prop_stridx(ctx, -2, DUK_STRIDX_FILE_NAME);

The proposed change fixes the issue and also removes the unnecessary forcing of the fileName, which is nice. I'll merge a fix soon.

svaarala added a commit that referenced this issue Oct 29, 2014
svaarala added a commit that referenced this issue Oct 29, 2014
Fix fileName for functions defined by modules loaded using require().
Although the fileName for the (Duktape internal) module function was set
to the resolved module ID, any functions defined in the module woult not
be affected by this and fileName would default to "duk_bi_global.c" which
is highly misleading.  See GH-58.
@svaarala
Copy link
Owner

The merge should fix the issue, and the following testcase was added to check for the preferred behavior at this point:

19491ef

@svaarala
Copy link
Owner

@emil-e It'd be nice to hear if the merge (in master) fixed this issue for you. Quite probably so because the fix is practically the one you suggested originally, but still :)

@emil-e
Copy link
Author

emil-e commented Oct 30, 2014

I'll check it out :)

On Thursday, October 30, 2014, Sami Vaarala notifications@github.com
wrote:

@emil-e https://github.com/emil-e It'd be nice to hear if the merge (in
master) fixed this issue for you. Quite probably so because the fix is
practically the one you suggested originally, but still :)


Reply to this email directly or view it on GitHub
#58 (comment).

svaarala added a commit that referenced this issue Oct 31, 2014
@svaarala
Copy link
Owner

This is now also fixed in 1.0.1 release.

@creationix
Copy link

Awesome thanks. I wanted this patch, but I'm trying to be good and stick to released versions.

@svaarala svaarala added the bug label Nov 2, 2014
@andoma
Copy link
Contributor

andoma commented Nov 5, 2014

FWIW, I use 1.0.1 in https://github.com/andoma/showtime/ and there it now prints backtraces as it should.

@svaarala
Copy link
Owner

svaarala commented Nov 8, 2014

@emil-e I'll close this now because it seems to be fixed - if not, please reopen.

@svaarala svaarala closed this as completed Nov 8, 2014
@emil-e
Copy link
Author

emil-e commented Nov 8, 2014

Sorry for the delay, I haven't had time to test it since I've been
traveling but I can't imagine this not working. Andoma tested it and it
worked for him so I'm perfectly fine with closing. Thanks for the great
response!

On Friday, November 7, 2014, Sami Vaarala notifications@github.com wrote:

Closed #58 #58.


Reply to this email directly or view it on GitHub
#58 (comment).

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

No branches or pull requests

4 participants