mguimas

mguimas

A module X in file /project/lib/x.ex has the following module metadata:

iex(7)> X.module_info()
[
  module: X,
  exports: [__info__: 1, foo: 0, module_info: 0, module_info: 1],
  attributes: [vsn: [259318855844826186809719652690816251721]],
  compile: [
    version: '7.3.1',
    options: [:dialyzer, :no_spawn_compiler_process, :from_core,
     :no_auto_import],
    source: '/project/lib/x.ex'
  ],
  native: false,
  md5: <<195, 22, 254, 181, 205, 81, 155, 43, 204, 133, 244, 77, 11, 5, 183,
    73>>
]

How can one modify the source entry to something else than the name of the file where module X is defined?

I tried

defmodule X do
@compile {:source, "/project/lib/another_file.ex"}
def foo, do: IO.puts "foo"
end

but this has no effect.

Thanks

Showing Posts 1 to 9

NobbZ

NobbZ

@file if I recall correctly. But it’s meant to be used for generated files only, as well as @line.

mguimas

mguimas OP

@file works on a function basis, not for the whole module unfortunately.

tty

tty

The closes I know is to strip the beam.

:beam_lib.strip_files(["Elixir.X.beam"]).

Honestly though I’m not sure why you would want to manipulate the source compile attribute. source and vsn are 2 quick checks in production that you are running the modules you think you should be running.

mguimas

mguimas OP

I want to manipulate it in generated modules, not in those written by hand of course.

michalmuskala

michalmuskala

The solution might be to use Module.create explicitly instead of using defmodule - you can alter the file in the environment passed in there.

mguimas

mguimas OP

I thought about that but there are issues.

For example, if we do like this

q = quote do
def foo, do: 1/0
def bar, do: 2/0
end
Module.create(Y, q, Map.put(__ENV__, :file, "y.ex"))

then when calling Y.foo() and Y.bar() the lines in the stracktraces are the same. I think this is because quote does not add metadata line: number in this case (only when the quotes are generated by the compiler when calling macros do they get the line: number metadata).

I also tried to do quote location: :keep do ... but then the file is the physical file, not y.ex.

The only solution I found is to do Macro.postwalk to add the metadata keep: {"y.ex", number} on every node that has the metadata line: number.

This solution is working well, but a more direct way would be preferable. The possibilities are:

  1. have @file set the file for all following functions and macros, not only the next one

  2. support quote location: "filepath" do and quote lines: true do

In fact, I think both points 1) and 2) would be useful for my and other use cases.

OvermindDL1

OvermindDL1

quote doesn’t, but calling a macro with AST does.

mguimas

mguimas OP

Yep, that’s what I said (unless I expressed myself badly :smile:)

OvermindDL1

OvermindDL1

Just clarifying that quoting into something is not the same as building Macro’s in Elixir (which is oddly different from other languages). ^.^;

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New

Other Trending Topics Top

mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews